1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-11-17 15:07:10 +00:00
erc-c/tests/mos6502.dis.c

237 lines
6.6 KiB
C
Raw Normal View History

2017-12-29 05:47:36 +00:00
#include <criterion/criterion.h>
#include <unistd.h>
#include "mos6502.h"
2017-12-29 05:47:36 +00:00
#include "mos6502.dis.h"
#include "mos6502.enums.h"
/*
* BUFSIZ is the normal block-buffer size that a FILE stream would use
* (possibly amongst other things).
*/
static char buf[BUFSIZ];
2017-12-29 05:47:36 +00:00
/*
* This is the file stream we will be using to write our disassembly
* code into.
*/
2017-12-29 05:47:36 +00:00
static FILE *stream = NULL;
static mos6502 *cpu = NULL;
static vm_segment *mem = NULL;
2017-12-29 05:47:36 +00:00
static void
setup()
{
// Ok, so...there's some...trickery going on here. As you might
// guess by the file path being /dev/null.
stream = fopen("/dev/null", "w");
2017-12-29 05:47:36 +00:00
if (stream == NULL) {
perror("Could not open temporary file for mos6502 disassembly tests");
exit(1);
2017-12-29 05:47:36 +00:00
}
// The C standard library allows us to set an arbitrary buffer for a
// file stream. It also allows us to fully buffer the file stream,
// which means nothing is written to file until an fflush() or an
// fclose() is called (or something else I can't think of). So we
// can use the FILE abstraction to write our disassembly results
// into, but use an underlying string buffer that we can easily
// check with Criterion. Uh, unless we blow out the buffer size...
// don't do that :D
setvbuf(stream, buf, _IOFBF, BUFSIZ);
mem = vm_segment_create(MOS6502_MEMSIZE);
cpu = mos6502_create(mem, mem);
2017-12-29 05:47:36 +00:00
}
static void
teardown()
{
fclose(stream);
mos6502_free(cpu);
vm_segment_free(mem);
2017-12-29 05:47:36 +00:00
}
static void
assert_buf(const char *str)
{
// This will set the cursor position in the file back to the start
// of the file stream.
2017-12-29 05:47:36 +00:00
rewind(stream);
// Our actual assertion. The downside to doing it this way is that
// when Criterion flags an assertion failure, it'll highlight _this_
// line in the file, not in the test. It might be worth macroifying
// this code.
2017-12-29 05:47:36 +00:00
cr_assert_str_eq(buf, str);
// We're not sure what previous tests may have run, and where NUL
// characters were set therein, so to be safe we wipe out the full
// contents of the test buffer after every test.
memset(buf, 0, BUFSIZ);
2017-12-29 05:47:36 +00:00
}
TestSuite(mos6502_dis, .init = setup, .fini = teardown);
Test(mos6502_dis, operand)
{
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, ABS, 0x1234);
2017-12-29 05:47:36 +00:00
assert_buf("$1234");
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, ABX, 0x1234);
2017-12-29 05:47:36 +00:00
assert_buf("$1234,X");
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, ABY, 0x1234);
2017-12-29 05:47:36 +00:00
assert_buf("$1234,Y");
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, IMM, 0x12);
2017-12-29 05:47:36 +00:00
assert_buf("#$12");
2017-12-29 23:08:25 +00:00
mos6502_set(cpu, 0x1234, 0x48);
mos6502_set(cpu, 0x1235, 0x34);
2017-12-29 23:08:25 +00:00
// For JMPs and JSRs (and BRKs), this should be a label and not a
// literal value. So we need to test both the literal and
// jump-labeled version.
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, IND, 0x1234);
2017-12-29 05:47:36 +00:00
assert_buf("($1234)");
2017-12-29 23:08:25 +00:00
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, IDX, 0x12);
2017-12-29 05:47:36 +00:00
assert_buf("($12,X)");
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, IDY, 0x34);
2017-12-29 05:47:36 +00:00
assert_buf("($34),Y");
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, ZPG, 0x34);
assert_buf("$34");
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, ZPX, 0x34);
2017-12-29 05:47:36 +00:00
assert_buf("$34,X");
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, ZPY, 0x34);
2017-12-29 05:47:36 +00:00
assert_buf("$34,Y");
2017-12-29 05:47:36 +00:00
// These should both end up printing nothing
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, ACC, 0);
2017-12-29 05:47:36 +00:00
assert_buf("");
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 0, IMP, 0);
2017-12-29 05:47:36 +00:00
assert_buf("");
2017-12-29 23:08:25 +00:00
// Test a forward jump (operand < 128)
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 500, REL, 52);
assert_buf("<0228>");
2017-12-29 23:08:25 +00:00
// Test a backward jump (operand >= 128)
2018-01-19 06:34:57 +00:00
mos6502_dis_operand(cpu, buf, sizeof(buf), 500, REL, 152);
assert_buf("<018c>");
2017-12-29 05:47:36 +00:00
}
Test(mos6502_dis, instruction)
{
#define TEST_INST(x) \
2018-01-19 06:34:57 +00:00
mos6502_dis_instruction(buf, sizeof(buf) - 1, x); \
2017-12-29 05:47:36 +00:00
assert_buf(#x)
TEST_INST(ADC);
TEST_INST(AND);
TEST_INST(ASL);
TEST_INST(BCC);
TEST_INST(BCS);
TEST_INST(BEQ);
TEST_INST(BIT);
TEST_INST(BMI);
TEST_INST(BNE);
TEST_INST(BPL);
TEST_INST(BRK);
TEST_INST(BVC);
TEST_INST(BVS);
TEST_INST(CLC);
TEST_INST(CLD);
TEST_INST(CLI);
TEST_INST(CLV);
TEST_INST(CMP);
TEST_INST(CPX);
TEST_INST(CPY);
TEST_INST(DEC);
TEST_INST(DEX);
TEST_INST(DEY);
TEST_INST(EOR);
TEST_INST(INC);
TEST_INST(INX);
TEST_INST(INY);
TEST_INST(JMP);
TEST_INST(JSR);
TEST_INST(LDA);
TEST_INST(LDX);
TEST_INST(LDY);
TEST_INST(LSR);
TEST_INST(NOP);
TEST_INST(ORA);
TEST_INST(PHA);
TEST_INST(PHP);
TEST_INST(PLA);
TEST_INST(PLP);
TEST_INST(ROL);
TEST_INST(ROR);
TEST_INST(RTI);
TEST_INST(RTS);
TEST_INST(SBC);
TEST_INST(SEC);
TEST_INST(SED);
TEST_INST(SEI);
TEST_INST(STA);
TEST_INST(STX);
TEST_INST(STY);
TEST_INST(TAX);
TEST_INST(TAY);
TEST_INST(TSX);
TEST_INST(TXA);
TEST_INST(TXS);
TEST_INST(TYA);
}
Test(mos6502_dis, expected_bytes)
{
#define TEST_BYTES(x, y) \
cr_assert_eq(mos6502_dis_expected_bytes(x), y)
TEST_BYTES(ACC, 0);
TEST_BYTES(ABS, 2);
TEST_BYTES(ABX, 2);
TEST_BYTES(ABY, 2);
TEST_BYTES(IMM, 1);
TEST_BYTES(IMP, 0);
TEST_BYTES(IND, 2);
TEST_BYTES(IDX, 1);
TEST_BYTES(IDY, 1);
TEST_BYTES(REL, 1);
TEST_BYTES(ZPG, 1);
TEST_BYTES(ZPX, 1);
TEST_BYTES(ZPY, 1);
}
Test(mos6502_dis, opcode)
2017-12-29 05:47:36 +00:00
{
int bytes;
mos6502_set(cpu, 0, 0x29); // AND (imm)
mos6502_set(cpu, 1, 0x38);
2017-12-29 05:47:36 +00:00
bytes = mos6502_dis_opcode(cpu, stream, 0);
2018-03-13 05:12:52 +00:00
assert_buf("0000:29 38 AND #$38 ; A:00 X:00 Y:00 P:C7<NV___IZC> S:FF\n");
2017-12-29 05:47:36 +00:00
cr_assert_eq(bytes, 2);
}
Test(mos6502_dis, scan)
{
mos6502_set(cpu, 0, 0x29); // AND (imm)
mos6502_set(cpu, 1, 0x38);
mos6502_set(cpu, 2, 0x88); // DEY (imp)
mos6502_set(cpu, 3, 0x6C); // JMP (ind)
mos6502_set(cpu, 4, 0x34);
mos6502_set(cpu, 5, 0x12);
mos6502_dis_scan(cpu, stream, 0, 6);
// FIXME: scan does not currently advance the PC byte; _should_ it?
// I'm honestly not sure. There are definitely times (e.g. during
// runtime operation) when you don't want it to, but as a standalone
// disassembler, it feels less useful when PC isn't emulated.
2018-03-13 05:12:52 +00:00
assert_buf("0000:29 38 AND #$38 ; A:00 X:00 Y:00 P:C7<NV___IZC> S:FF\n"
"0002:88 DEY ; A:00 X:00 Y:00 P:C7<NV___IZC> S:FF\n"
"0003:6C 34 12 JMP ($1234) ; A:00 X:00 Y:00 P:C7<NV___IZC> S:FF\n");
}