From 927d03ebbe36c2dcd8a0d1554f113390ffe314f9 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Fri, 29 Dec 2017 15:31:05 -0600 Subject: [PATCH] Add scan function (rename original scan -> opcode); test --- include/mos6502.dis.h | 3 ++- src/mos6502.dis.c | 24 +++++++++++++++++------- tests/mos6502.dis.c | 25 +++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/include/mos6502.dis.h b/include/mos6502.dis.h index a9fce07..98d2561 100644 --- a/include/mos6502.dis.h +++ b/include/mos6502.dis.h @@ -5,8 +5,9 @@ #include "vm_segment.h" extern int mos6502_dis_expected_bytes(vm_8bit); -extern int mos6502_dis_scan(FILE *, vm_segment *, int); +extern int mos6502_dis_opcode(FILE *, vm_segment *, int); extern void mos6502_dis_instruction(FILE *, int); extern void mos6502_dis_operand(FILE *, int, vm_16bit); +extern void mos6502_dis_scan(FILE *, vm_segment *, int, int); #endif diff --git a/src/mos6502.dis.c b/src/mos6502.dis.c index 86cf2c0..3c956b2 100644 --- a/src/mos6502.dis.c +++ b/src/mos6502.dis.c @@ -186,7 +186,7 @@ mos6502_dis_expected_bytes(int addr_mode) * of bytes consumed by scanning past the opcode and/or operand. */ int -mos6502_dis_scan(FILE *stream, vm_segment *segment, int address) +mos6502_dis_opcode(FILE *stream, vm_segment *segment, int address) { vm_8bit opcode; vm_16bit operand; @@ -240,13 +240,15 @@ mos6502_dis_scan(FILE *stream, vm_segment *segment, int address) // Print out the instruction code that our opcode represents. mos6502_dis_instruction(stream, mos6502_instruction(opcode)); - // Let's "tab" over; each instruction code is 3 characters, so let's - // move over 5 spaces (4 spaces indent + 1, just to keep everything - // aligned by 4-character boundaries). - fprintf(stream, " "); + if (expected) { + // Let's "tab" over; each instruction code is 3 characters, so let's + // move over 5 spaces (4 spaces indent + 1, just to keep everything + // aligned by 4-character boundaries). + fprintf(stream, " "); - // Print out the operand given the proper address mode. - mos6502_dis_operand(stream, mos6502_addr_mode(opcode), operand); + // Print out the operand given the proper address mode. + mos6502_dis_operand(stream, mos6502_addr_mode(opcode), operand); + } // And let's terminate the line. fprintf(stream, "\n"); @@ -256,3 +258,11 @@ mos6502_dis_scan(FILE *stream, vm_segment *segment, int address) // opcode sequence would consume. return expected + 1; } + +void +mos6502_dis_scan(FILE *stream, vm_segment *segment, int pos, int end) +{ + while (pos < end) { + pos += mos6502_dis_opcode(stream, segment, pos); + } +} diff --git a/tests/mos6502.dis.c b/tests/mos6502.dis.c index 4601805..33e3801 100644 --- a/tests/mos6502.dis.c +++ b/tests/mos6502.dis.c @@ -164,7 +164,7 @@ Test(mos6502_dis, expected_bytes) TEST_BYTES(ZPY, 1); } -Test(mos6502_dis, scan) +Test(mos6502_dis, opcode) { vm_segment *segment; int bytes; @@ -174,7 +174,28 @@ Test(mos6502_dis, scan) vm_segment_set(segment, 0, 0x29); // AND (imm) vm_segment_set(segment, 1, 0x38); - bytes = mos6502_dis_scan(stream, segment, 0); + bytes = mos6502_dis_opcode(stream, segment, 0); assert_buf(" AND #$38\n"); cr_assert_eq(bytes, 2); } + +Test(mos6502_dis, scan) +{ + vm_segment *segment; + + segment = vm_segment_create(1000); + + vm_segment_set(segment, 0, 0x29); // AND (imm) + vm_segment_set(segment, 1, 0x38); + vm_segment_set(segment, 2, 0x88); // DEY (imp) + vm_segment_set(segment, 3, 0x6C); // JMP (ind) + vm_segment_set(segment, 4, 0x12); + vm_segment_set(segment, 5, 0x34); + + mos6502_dis_scan(stream, segment, 0, 6); + + assert_buf(" AND #$38\n" + " DEY\n" + " JMP ($1234)\n" + ); +}