From ab9dd4e1b7227cb0ceda809a568aaf94297b0cc6 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 5 May 2021 16:44:30 -0600 Subject: [PATCH] Updated 16bit counter example with higher level functions --- examples/16bit_counter.cpp | 57 ++++++++++++++++++++------------------ src/main.cpp | 11 +++++++- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/examples/16bit_counter.cpp b/examples/16bit_counter.cpp index 118899f..162ec55 100644 --- a/examples/16bit_counter.cpp +++ b/examples/16bit_counter.cpp @@ -34,21 +34,29 @@ inline void puts(uint8_t x, uint8_t y, std::string_view str) { str.size()); } - -inline void put_hex(uint8_t x, uint8_t y, uint8_t value) -{ - -} - -inline void put_hex(uint8_t x, uint8_t y, uint16_t value) -{ -} - inline void putc(uint8_t x, uint8_t y, uint8_t c) { const auto start = 0x400 + (y * 40 + x); poke(start, c); } +inline void put_hex(uint8_t x, uint8_t y, uint8_t value) { + const auto put_nibble = [](auto x, auto y, uint8_t nibble) { + if (nibble <= 9) { + putc(x, y, nibble + 48); + } else { + putc(x, y, nibble - 9); + } + }; + + put_nibble(x + 1, y, 0xF & value); + put_nibble(x, y, 0xF & (value >> 4)); +} + +inline void put_hex(uint8_t x, uint8_t y, uint16_t value) { + put_hex(x+2,y, static_cast(0xFF & value)); + put_hex(x,y, static_cast(0xFF & (value >> 8))); +} + struct Clock { using milliseconds = std::chrono::duration; @@ -83,29 +91,24 @@ int main() { /* puts(5, 5, "hello world"); puts(10, 10, "hellooooo world"); +*/ + Clock game_clock{}; + - Clock game_clock{}; - */ + std::uint16_t counter = 0; - std::uint16_t us_elapsed = 0; - - int y = 0; + std::uint8_t y = 15; while (true) { - // const auto us_elapsed = game_clock.restart().count(); + const auto us_elapsed = game_clock.restart().count(); - for (int i = 0; i < 4; ++i) { - const auto nibble = static_cast((us_elapsed >> (i * 4)) & 0xF); - if (nibble <= 9) { - putc(6 - i, y, nibble + 48); - } else { - putc(6 - i, y, nibble - 9); - } + put_hex(5, y, us_elapsed); + put_hex(11, y, counter); + + if (y++ == 20) { + y = 15; } - if (y++ == 5) { - y = 0; - } - ++us_elapsed; + ++counter; increment_border_color(); } diff --git a/src/main.cpp b/src/main.cpp index 7a341b6..0e0a235 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1072,9 +1072,10 @@ void translate_instruction(std::vector &instructions, const AVR::OpCode instructions.emplace_back(mos6502::OpCode::bcs, Operand(Operand::Type::literal, new_label_name)); instructions.emplace_back(ASMLine::Type::Directive, new_label_name); return; + } else { + instructions.emplace_back(mos6502::OpCode::bcs, o1); } - throw std::runtime_error("Unable to handle unknown brsh offset"); } case AVR::OpCode::breq: { @@ -1568,6 +1569,14 @@ bool fix_long_branches(std::vector &instructions, int &branch_patch_cou instructions.insert(std::next(std::begin(instructions), op + 2), mos6502(ASMLine::Type::Label, new_pos)); instructions[op].comment = instructions[op + 1].comment = instructions[op + 2].comment = comment; return true; + } else if (instructions[op].opcode == mos6502::OpCode::bcs) { + const auto comment = instructions[op].comment; + instructions[op] = mos6502(mos6502::OpCode::bcc, Operand(Operand::Type::literal, new_pos)); + instructions.insert(std::next(std::begin(instructions), op + 1), + mos6502(mos6502::OpCode::jmp, Operand(Operand::Type::literal, going_to))); + instructions.insert(std::next(std::begin(instructions), op + 2), mos6502(ASMLine::Type::Label, new_pos)); + instructions[op].comment = instructions[op + 1].comment = instructions[op + 2].comment = comment; + return true; } else { throw std::runtime_error("Don't know how to reorg this branch: " + instructions[op].to_string()); }