1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-08-21 00:29:00 +00:00

Add scan function (rename original scan -> opcode); test

This commit is contained in:
Peter Evans 2017-12-29 15:31:05 -06:00
parent f98e2f65ee
commit 927d03ebbe
3 changed files with 42 additions and 10 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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"
);
}