I started writing some C code to see if I can do what I want it to. It didn't take long to realize that I was really just writing an assembler. However, it's an assembler where I can write my own macros, so it's pretty handy. The macros really end up looking like instructions. I use a giant array to hold the code. This is easier than file I/O and string processing. And each memory location gets an optional label in case I want to jump there. My one instruction routine is just:
void move(uint16_t src, uint16_t dst)
{
code[codeIndex].byte = src;
codeIndex++;
code[codeIndex].byte = dst;
codeIndex++;
}
And I can make macros like this:/* load: moves a constant into a destination */ void load(uint16_t constant, uint16_t dst) { move(constant,LOAD); move(LOAD,dst); }I can add labels and jump to them:
void label(char labelstring[20])
{
strcpy(code[codeIndex].label,labelstring);
}
void jump(char labelstring[20])
{
uint16_t i;
for (i=codeStart;i<codeIndex;i+=2)
{
if (strcmp(labelstring,code[i].label)==0)
{
load((i/256),PCTEMP);
load((i%256),PCLO);
return;
}
}
strcpy(code[codeIndex].missingUpper,labelstring);
load(0x00,PCTEMP);
strcpy(code[codeIndex].missingLower,labelstring);
load(0x00,PCLO);
}
As you can see I need to handle missing labels. So I do a second pass (just like a real assembler) afterwards to find the missing labels and insert them into the code.
So my programs end up looking like this:void aluTester(uint16_t start)
{
if (0!=start)
{
codeStart = start;
codeIndex = codeStart;
}
label("ALU_TEST");
load(0x35, ALUA);
load(0x03, SUB);
move(SUB, UDAT);
jump("ALU_TEST");
}
This is much more readable and usable than writing in just assembly. The start variable is the address to put the code in memory. All of this allows me to write macros the "right way". I was hacking in the code to make it small, but not as modular. For example, if I knew I was jumping within the same page, I wouldn't update the page register (PCTEMP). With this code, it always writes the page register before a jump. So the programs end up being longer. However, I can always go back and optimize the jump routine to keep track of what page it is on and optionally write there more intelligently. This is already working so well, I believe it's my path forward. I have one simple function which outputs an easy-to-read display of instructions, and one which will output in a format that I can copy/paste to the bootloader running on the FPGA dev board. We are really rolling now!
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.