Skip to content

Instantly share code, notes, and snippets.

@brouhaha
Last active July 14, 2025 06:34
Show Gist options
  • Select an option

  • Save brouhaha/73679e363a7a22047fa4849b3b7750dd to your computer and use it in GitHub Desktop.

Select an option

Save brouhaha/73679e363a7a22047fa4849b3b7750dd to your computer and use it in GitHub Desktop.
O2test main program for running 6502_functional_test.bin
// o2_test.cc
//
// Copyright 2022-2025 Eric Smith
// SPDX-License-Identifier: GPL-3.0-only
#include <cstdint>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include "O2.hpp"
bool skip_decimal_test = true;
std::uint16_t test_load_address = 0x0000;
std::uint16_t test_execution_address = 0x0400;
std::uint16_t test_patch_address = 0x3361;
std::uint16_t test_patch_target_address = 0x345d;
static constexpr std::uint8_t JMP_ABS_OPCODE = 0x4c;
std::array<std::uint8_t, 0x10000> memory;
std::uint8_t read(std::uint16_t addr)
{
return memory[addr];
}
void write(std::uint16_t addr, std::uint8_t data)
{
memory[addr] = data;
}
void load_raw_bin(const std::filesystem::path object_filename,
std::uint16_t load_address)
{
std::ifstream object_file(object_filename);
if (! object_file.is_open())
{
throw std::runtime_error("can't open object file");
}
char c;
std::size_t length;
while (true)
{
c = object_file.get();
if (object_file.eof())
{
break;
}
if (object_file.fail())
{
throw std::runtime_error("error reading object file");
}
length++;
memory[load_address++] = static_cast<std::uint8_t>(c);
}
std::cout << std::format("loaded {} (0x{:04x}) bytes\n", length, length);
}
int main([[maybe_unused]] int argc,
[[maybe_unused]] char *argv[])
{
load_raw_bin("6502_functional_test.bin", test_load_address);
if (skip_decimal_test)
{
// patch preasembled functional test binary to skip decimal test: ORG $346f, JMP $35a2
write(test_patch_address , JMP_ABS_OPCODE);
write(test_patch_address + 1, test_patch_target_address & 0xff);
write(test_patch_address + 2, test_patch_target_address >> 8);
}
O2::CPU processor(read, write);
processor.step(); // get past reset
processor.PC = test_execution_address;
while (true)
{
std::uint16_t prev_pc = processor.PC;
processor.step();
if (processor.PC == prev_pc)
{
std::cout << std::format("halt at {:04x}\n", processor.PC);
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment