Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@keyboardsmoke
Created September 1, 2017 04:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyboardsmoke/cffbec697448250ba8a47fe2ebee8e1a to your computer and use it in GitHub Desktop.
Save keyboardsmoke/cffbec697448250ba8a47fe2ebee8e1a to your computer and use it in GitHub Desktop.
// pld.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
struct PLD {
unsigned addr_mode : 12;
unsigned z0 : 4; // 1111
unsigned Rn : 4;
unsigned z1 : 3; // 101
unsigned U : 1;
unsigned z2 : 1; // 1
unsigned I : 1;
unsigned z3 : 2; // 01
unsigned cond : 4; // 1111
};
static_assert(sizeof(PLD) == 4, "Invalid size");
void print_bits(uint32_t ins)
{
for (int i = 0; i < (sizeof(uint32_t) * 8); i++)
{
if (i == 12 ||
i == 16 ||
i == 20 ||
i == 23 ||
i == 24 ||
i == 25 ||
i == 26 ||
i == 28)
printf(":");
printf("%i", ins & 0x01);
ins = ins >> 1;
}
}
int main()
{
PLD ins;
// the "trick" instruction i posted on my blog
// A normal PLD is commented
unsigned char uc[4] = { 0x02, 0xDF, 0xDF, 0xF5 /* 0x02, 0xF0, 0xDF, 0xF5 */ };
memcpy(&ins, uc, 4);
printf("pld = ");
print_bits(*(uint32_t*)&ins);
printf("\n");
PLD output;
output.addr_mode = 0b101010101010;
output.z0 = 0b0000; // Making this *not* 1111 makes the instruction malformed.
output.Rn = 0b1111;
output.z1 = 0b101;
output.U = 0b0;
output.z2 = 0b1;
output.I = 0b1;
output.z3 = 0b01;
output.cond = 0b1111;
printf("output = ");
print_bits(*(uint32_t*)&output);
printf("\n");
memcpy(uc, &output, 4);
printf("uc = { 0x%X, 0x%X, 0x%X, 0x%X }\n", uc[0], uc[1], uc[2], uc[3]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment