Skip to content

Commit

Permalink
Add data/code/function information to PCEAS's tracking of ROM output.
Browse files Browse the repository at this point in the history
Destructively sort the symbol table before output.

Add an initial work-in-progress version of a modified form of wla-dx's sym file
source-level debugging output for use with mesen2.

Change various symbol structure members from ints into unsigned chars to save a
little bit of memory.
  • Loading branch information
jbrandwood committed Nov 14, 2024
1 parent 3d04a39 commit ee7843b
Show file tree
Hide file tree
Showing 14 changed files with 539 additions and 149 deletions.
100 changes: 52 additions & 48 deletions src/mkit/as/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class1(int *ip)
/* generate code */
if (pass == LAST_PASS) {
/* opcode */
putbyte(data_loccnt, opval);
putbyte(data_loccnt, opval, CODE_OUT);

/* output line */
println();
Expand Down Expand Up @@ -136,19 +136,19 @@ class2(int *ip)
/* long-branch opcode */
if ((opval & 0x1F) == 0x10) {
/* conditional-branch */
putbyte(data_loccnt + 0, opval ^ 0x20);
putbyte(data_loccnt + 1, 0x03);
putbyte(data_loccnt + 0, opval ^ 0x20, CODE_OUT);
putbyte(data_loccnt + 1, 0x03, CODE_OUT);

putbyte(data_loccnt + 2, 0x4C);
putword(data_loccnt + 3, value & 0xFFFF);
putbyte(data_loccnt + 2, 0x4C, CODE_OUT);
putword(data_loccnt + 3, value & 0xFFFF, CODE_OUT);
} else {
/* bra and bsr */
putbyte(data_loccnt + 0, (opval == 0x80) ? 0x4C : 0x20);
putword(data_loccnt + 1, value & 0xFFFF);
putbyte(data_loccnt + 0, (opval == 0x80) ? 0x4C : 0x20, CODE_OUT);
putword(data_loccnt + 1, value & 0xFFFF, CODE_OUT);
}
} else {
/* short-branch opcode */
putbyte(data_loccnt, opval);
putbyte(data_loccnt, opval, CODE_OUT);

/* calculate branch offset */
addr = (value & 0xFFFF) - (loccnt + (page << 13) + phase_offset);
Expand All @@ -160,7 +160,7 @@ class2(int *ip)
}

/* offset */
putbyte(data_loccnt + 1, addr);
putbyte(data_loccnt + 1, addr, CODE_OUT);
}

/* output line */
Expand All @@ -186,8 +186,8 @@ class3(int *ip)
/* generate code */
if (pass == LAST_PASS) {
/* opcodes */
putbyte(data_loccnt, opval);
putbyte(data_loccnt + 1, optype);
putbyte(data_loccnt, opval, CODE_OUT);
putbyte(data_loccnt + 1, optype, CODE_OUT);

/* output line */
println();
Expand Down Expand Up @@ -263,8 +263,8 @@ class4(int *ip)
/* auto-tag */
if (auto_tag) {
if (pass == LAST_PASS) {
putbyte(loccnt, 0xA0);
putbyte(loccnt + 1, auto_tag_value);
putbyte(loccnt, 0xA0, CODE_OUT);
putbyte(loccnt + 1, auto_tag_value, CODE_OUT);
}
loccnt += 2;
}
Expand All @@ -274,7 +274,7 @@ class4(int *ip)
case ACC:
/* one byte */
if (pass == LAST_PASS)
putbyte(loccnt, opval);
putbyte(loccnt, opval, CODE_OUT);

loccnt++;
break;
Expand All @@ -288,8 +288,8 @@ class4(int *ip)
case ZP_IND_Y:
/* two bytes */
if (pass == LAST_PASS) {
putbyte(loccnt, opval);
putbyte(loccnt + 1, value);
putbyte(loccnt, opval, CODE_OUT);
putbyte(loccnt + 1, value, CODE_OUT);
}
loccnt += 2;
break;
Expand All @@ -299,10 +299,14 @@ class4(int *ip)
case ABS_Y:
case ABS_IND:
case ABS_IND_X:
/* remember what labels are function calls */
if (opval == 0x20 && expr_lablptr != NULL)
expr_lablptr->flags |= FLG_FUNCTION;

/* three bytes */
if (pass == LAST_PASS) {
putbyte(loccnt, opval);
putword(loccnt + 1, value);
putbyte(loccnt, opval, CODE_OUT);
putword(loccnt + 1, value, CODE_OUT);
}
loccnt += 3;
break;
Expand All @@ -311,7 +315,7 @@ class4(int *ip)
/* auto-increment */
if (auto_inc) {
if (pass == LAST_PASS)
putbyte(loccnt, auto_inc);
putbyte(loccnt, auto_inc, CODE_OUT);

loccnt += 1;
}
Expand Down Expand Up @@ -361,16 +365,16 @@ class5(int *ip)
if (pass == LAST_PASS) {
if ((branch) && (branch->convert)) {
/* long-branch opcode */
putbyte(data_loccnt, opval ^ 0x80);
putbyte(data_loccnt + 1, zp);
putbyte(data_loccnt + 2, 0x03);
putbyte(data_loccnt, opval ^ 0x80, CODE_OUT);
putbyte(data_loccnt + 1, zp, CODE_OUT);
putbyte(data_loccnt + 2, 0x03, CODE_OUT);

putbyte(data_loccnt + 3, 0x4C);
putword(data_loccnt + 4, value & 0xFFFF);
putbyte(data_loccnt + 3, 0x4C, CODE_OUT);
putword(data_loccnt + 4, value & 0xFFFF, CODE_OUT);
} else {
/* short-branch opcode */
putbyte(data_loccnt, opval);
putbyte(data_loccnt + 1, zp);
putbyte(data_loccnt, opval, CODE_OUT);
putbyte(data_loccnt + 1, zp, CODE_OUT);

/* calculate branch offset */
addr = (value & 0xFFFF) - (loccnt + (page << 13) + phase_offset);
Expand All @@ -382,7 +386,7 @@ class5(int *ip)
}

/* offset */
putbyte(data_loccnt + 2, addr);
putbyte(data_loccnt + 2, addr, CODE_OUT);
}

/* output line */
Expand Down Expand Up @@ -422,10 +426,10 @@ class6(int *ip)
/* generate code */
if (pass == LAST_PASS) {
/* opcodes */
putbyte(data_loccnt, opval);
putword(data_loccnt + 1, addr[0]);
putword(data_loccnt + 3, addr[1]);
putword(data_loccnt + 5, addr[2]);
putbyte(data_loccnt, opval, CODE_OUT);
putword(data_loccnt + 1, addr[0], CODE_OUT);
putword(data_loccnt + 3, addr[1], CODE_OUT);
putword(data_loccnt + 5, addr[2], CODE_OUT);

/* output line */
println();
Expand Down Expand Up @@ -468,15 +472,15 @@ class7(int *ip)
/* generate code */
if (pass == LAST_PASS) {
/* opcodes */
putbyte(loccnt, opval);
putbyte(loccnt + 1, imm);
putbyte(loccnt, opval, CODE_OUT);
putbyte(loccnt + 1, imm, CODE_OUT);

if (mode & (ZP | ZP_X))
/* zero page */
putbyte(loccnt + 2, addr);
putbyte(loccnt + 2, addr, CODE_OUT);
else
/* absolute */
putword(loccnt + 2, addr);
putword(loccnt + 2, addr, CODE_OUT);
}

/* update location counter */
Expand All @@ -488,7 +492,7 @@ class7(int *ip)
/* auto-increment */
if (auto_inc) {
if (pass == LAST_PASS)
putbyte(loccnt, auto_inc);
putbyte(loccnt, auto_inc, CODE_OUT);

loccnt += 1;
}
Expand Down Expand Up @@ -527,8 +531,8 @@ class8(int *ip)
}

/* opcodes */
putbyte(data_loccnt, opval);
putbyte(data_loccnt + 1, (1 << value));
putbyte(data_loccnt, opval, CODE_OUT);
putbyte(data_loccnt + 1, (1 << value), CODE_OUT);

/* output line */
println();
Expand Down Expand Up @@ -571,8 +575,8 @@ class9(int *ip)
}

/* opcodes */
putbyte(data_loccnt, opval + (bit << 4));
putbyte(data_loccnt + 1, value);
putbyte(data_loccnt, opval + (bit << 4), CODE_OUT);
putbyte(data_loccnt + 1, value, CODE_OUT);

/* output line */
println();
Expand Down Expand Up @@ -632,16 +636,16 @@ class10(int *ip)

if ((branch) && (branch->convert)) {
/* long-branch opcode */
putbyte(data_loccnt, (opval + (bit << 4)) ^ 0x80);
putbyte(data_loccnt + 1, zp);
putbyte(data_loccnt + 2, 0x03);
putbyte(data_loccnt, (opval + (bit << 4)) ^ 0x80, CODE_OUT);
putbyte(data_loccnt + 1, zp, CODE_OUT);
putbyte(data_loccnt + 2, 0x03, CODE_OUT);

putbyte(data_loccnt + 3, 0x4C);
putword(data_loccnt + 4, value & 0xFFFF);
putbyte(data_loccnt + 3, 0x4C, CODE_OUT);
putword(data_loccnt + 4, value & 0xFFFF, CODE_OUT);
} else {
/* short-branch opcode */
putbyte(data_loccnt, opval + (bit << 4));
putbyte(data_loccnt + 1, zp);
putbyte(data_loccnt, opval + (bit << 4), CODE_OUT);
putbyte(data_loccnt + 1, zp, CODE_OUT);

/* calculate branch offset */
addr = (value & 0xFFFF) - (loccnt + (page << 13) + phase_offset);
Expand All @@ -653,7 +657,7 @@ class10(int *ip)
}

/* offset */
putbyte(data_loccnt + 2, addr);
putbyte(data_loccnt + 2, addr, CODE_OUT);
}

/* output line */
Expand Down
46 changes: 35 additions & 11 deletions src/mkit/as/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ unsigned short pseudo_allowed[] = {
/* P_3PASS */ IN_CODE + IN_HOME + IN_DATA + IN_ZP + IN_BSS,
/* P_ALIAS */ IN_CODE + IN_HOME + IN_DATA + IN_ZP + IN_BSS,
/* P_REF */ IN_CODE + IN_HOME + IN_DATA + IN_ZP + IN_BSS,
/* P_PHASE */ IN_CODE + IN_HOME
/* P_PHASE */ IN_CODE + IN_HOME,
/* P_LINE */ ANYWHERE
};


Expand Down Expand Up @@ -364,7 +365,7 @@ do_db(int *ip)
/* store char on last pass */
if (pass == LAST_PASS) {
/* store character */
putbyte(loccnt, c);
putbyte(loccnt, c, DATA_OUT);
}

/* update location counter */
Expand Down Expand Up @@ -401,7 +402,7 @@ do_db(int *ip)
}

/* store byte */
putbyte(loccnt - 1, value);
putbyte(loccnt - 1, value, DATA_OUT);
}
}

Expand Down Expand Up @@ -493,7 +494,7 @@ do_dw(int *ip)
}

/* store word */
putword(loccnt - 2, value);
putword(loccnt - 2, value, DATA_OUT);
}

/* check if there's another word */
Expand Down Expand Up @@ -576,7 +577,7 @@ do_dwl(int *ip)
}

/* store word */
putbyte(loccnt - 1, (value & 0xff));
putbyte(loccnt - 1, (value & 0xff), DATA_OUT);
}

/* check if there's another word */
Expand Down Expand Up @@ -659,7 +660,7 @@ do_dwh(int *ip)
}

/* store word */
putbyte(loccnt - 1, ((value >> 8) & 0xff));
putbyte(loccnt - 1, ((value >> 8) & 0xff), DATA_OUT);
}

/* check if there's another word */
Expand Down Expand Up @@ -1171,6 +1172,7 @@ do_incbin(int *ip)
/* load data on last pass */
if (pass == LAST_PASS) {
uint32_t info, *fill;
int is_code = DATA_OUT;

fread(&rom[bank][loccnt], 1, size, fp);

Expand Down Expand Up @@ -1356,7 +1358,7 @@ do_mx(char *fname)
/* copy data */
if (pass == LAST_PASS) {
for (i = 0; i < cnt; i++)
putbyte(loccnt + i, buffer[i]);
putbyte(loccnt + i, buffer[i], DATA_OUT);
}

/* update location counter */
Expand Down Expand Up @@ -1614,6 +1616,7 @@ do_ds(int *ip)
/* output line on last pass */
if (pass == LAST_PASS) {
uint32_t info, *fill;
int is_code = DATA_OUT;

if (filler != 0) {
if (section == S_ZP)
Expand Down Expand Up @@ -1949,6 +1952,7 @@ do_align(int *ip)
} else {
if (section == S_CODE || section == S_DATA) {
uint32_t info, *fill;
int is_code = DATA_OUT;

memset(&rom[bank][oldloc], 0, loccnt - oldloc);
memset(&map[bank][oldloc], section + (page << 5), loccnt - oldloc);
Expand Down Expand Up @@ -2455,7 +2459,7 @@ do_alias(int *ip)
error("Symbol already used by a label!");
return;
}
if (lablptr->reserved) {
if (lablptr->flags & FLG_RESERVED) {
error("Reserved symbol!");
return;
}
Expand Down Expand Up @@ -2516,7 +2520,7 @@ do_alias(int *ip)
error("Cannot create a .ALIAS to a function!");
return;
}
if (alias->reserved) {
if (alias->flags & FLG_RESERVED) {
error("Cannot create a .ALIAS to a reserved symbol!");
return;
}
Expand Down Expand Up @@ -2640,6 +2644,26 @@ do_phase(int *ip)
}


/* ----
* do_debug()
* ----
* .dbg pseudo for C source code debugging
*/

void
do_debug(int *ip)
{
/* set label value if there was one */
labldef(LOCATION);

/* output line on last pass */
if (pass == LAST_PASS) {
// loadlc(loccnt, 0);
println();
}
}


/* ----
* htoi()
* ----
Expand Down Expand Up @@ -2671,12 +2695,12 @@ htoi(char *str, int nb)


/* ----
* set_section(int new_section)
* set_section(unsigned char new_section)
* ----
*/

void
set_section(int new_section)
set_section(unsigned char new_section)
{
if (section != new_section) {
/* backup current section data */
Expand Down
Loading

0 comments on commit ee7843b

Please sign in to comment.