Refactoring - header messes addresses up, needs to be removed.
h3rald h3rald@h3rald.com
Thu, 04 Dec 2025 11:27:58 +0100
M
xywasm.c
→
xywasm.c
@@ -229,23 +229,19 @@ }
return 0; } -// Write raw bytecode data to a specified address -static inline void write(xyw_word addr, xyw_word val) +// Append raw bytecode data +static inline void append(xyw_word val) { if (val > 0xff) { - data[addr] = (val >> 8) & 0xFF; - data[addr + 1] = val & 0xFF; + XYW_POKEW(&data[ptr], val); ptr++; - return; + } + else + { + data[ptr] = (xyw_byte)val; } - data[addr] = (xyw_byte)val; -} - -// Append raw bytecode data -static inline void append(xyw_word val) -{ - write(ptr++, val); + ptr++; } // Encode a push instruction@@ -457,7 +453,6 @@
static int process_string(xyw_context *ctx) { (void)ctx; - xyw_dbg("String:\t\t%s\n", token); token_type = TOKEN_TYPE_STRING; return 0; }@@ -485,6 +480,7 @@ ctx->sp = NULL;
} else if (sym->type == SYMBOL_TYPE_LABEL) { + xyw_dbg("Label Ref:\t%s -> %04X\n", token, sym->address); // Push label address push(sym->address); }@@ -499,10 +495,14 @@ else
{ // Store reference to be resolved later xyw_dbg("Reference:\t%s\n", token); + // Push placeholder value $0000 for now + append(PSH | XYW_MODE_W); if (ref(ctx, token) != 0) { return -1; } + append(0x00); + append(0x00); } } return 0;@@ -521,7 +521,7 @@ if (process_symbol(ctx) != 0)
{ return -1; } - xyw_dbg("Directive:\t.label %s\n", token); + xyw_dbg("Directive:\t.label %s [$%04X]\n", token, ptr); label(ctx, token); return validate_single_directive_argument(ctx, r); }@@ -769,7 +769,8 @@ {
c = *ctx->sp++; } } - // Handle character + // printf("Char read: '%c' (0x%02X) [Ln %d, Col %d] - Token: %s\n", c, (unsigned char)c, ctx->line, ctx->column, token); + // Handle character if (c == '\n') { if (in_string)@@ -944,8 +945,8 @@ fclose(fp);
return -1; } // Patch the data at the reference address with the label address - write(refsym->address, defsym->address); xyw_dbg("Patch:\t\t%s -> $%04X @ $%04X\n", defsym->name, defsym->address, refsym->address); + XYW_POKEW(&data[refsym->address], defsym->address); } fwrite(data, 1, ptr, fp); fclose(fp);
M
xywrun.c
→
xywrun.c
@@ -340,11 +340,21 @@ fclose(fp);
return -1; } xyw_dbg("Loaded %zu bytes into memory\n", bytes_read); + // Print loaded bytes, including header, 16 per line + xyw_dbg("Memory dump:\n"); + for (size_t i = 0; i < bytes_read; i++) + { + xyw_dbg("%02X ", xyw_memory[USER_MEMORY_START + i]); + if ((i + 1) % 16 == 0) + { + xyw_dbg("\n"); + } + } fclose(fp); xyw_memory[SYSTEM_STATE] = 1; while (xyw_memory[SYSTEM_STATE] && *pc < SYSTEM_MEMORY_START) { - xyw_dbg("PC: %04X -> %02X (%s)\n", *pc, xyw_memory[*pc], xyw_instructions[xyw_memory[*pc] & 0x1F]); + xyw_dbg("PC: %04X -> %02X (%s) [X:%02X|Y:%02X|XW:%04X|YW:%04X]\n", *pc, xyw_memory[*pc], xyw_instructions[xyw_memory[*pc] & 0x1F], *x, *y, *xw, *yw); switch (xyw_memory[*pc]) { // clang-format off@@ -378,11 +388,13 @@ /* OVR */ OPC_SR2(0x1A, PUSH(b); PUSH(a); PUSH(b));
/* ROT */ OPC_SR2(0x1B, PUSH(b); PUSH(POP()); PUSH(a)); /* JMP */ case 0x1C: *pc = us_popw(); break; /* JCN */ OPC_SR1(0x1D, , if (ARGXY) { *pc = POP(); } ); - /* JSR */ case 0x1E: xyw_word addr = us_popw(); ss_pushw(*pc); *pc = addr; break; - /* RTS */ case 0x1F: *pc = ss_popw(); break; + /* JSR */ case 0x1E: xyw_word addr = us_popw(); ss_pushw(*pc); *pc = addr-1; break; + /* RTS */ case 0x1F: *pc = ss_popw()-1; break; // clang-format on } // TODO: error handling etc. + // At present, JSR and RTS set the pc to 1 value less than the actual address + // because we are relying on the fact that pc will be incremented at the end of the loop (*pc)++; } return 0;