1
0
mirror of https://github.com/lefticus/6502-cpp.git synced 2024-12-21 10:30:35 +00:00

Fix processing of labels to get std::variant working

This commit is contained in:
Jason Turner 2021-05-25 13:49:33 -06:00
parent 5b764ad179
commit 1f03d18d5e
3 changed files with 73 additions and 18 deletions

View File

@ -4,6 +4,7 @@
#include <cstring>
#include <string_view>
#include <span>
#include <variant>
#include "chargen.hpp"
@ -285,6 +286,7 @@ struct SimpleSprite
}
};
struct Screen
{
void load(std::uint8_t x, std::uint8_t y, auto &s) {
@ -405,10 +407,36 @@ int main() {
std::uint8_t stamina{max_stamina()};
std::uint16_t cash{100};
constexpr std::uint8_t max_stamina() const {
Clock game_clock{};
constexpr std::uint8_t max_stamina() const noexcept {
return endurance * 5;
}
struct JoyStickStateChanged
{
std::uint8_t state;
};
struct TimeElapsed
{
Clock::milliseconds us;
};
using Event = std::variant<JoyStickStateChanged, TimeElapsed>;
std::uint8_t last_joystick_state = peek(0xDC00);
Event next_event() noexcept {
const auto new_joystick_state = peek(0xDC00);
if (new_joystick_state != last_joystick_state) {
last_joystick_state = new_joystick_state;
return JoyStickStateChanged{new_joystick_state};
}
return TimeElapsed{game_clock.restart()};
}
};
GameState game;
@ -428,22 +456,33 @@ int main() {
put_hex(12,23, cur_game.cash);
};
Clock game_clock{};
Screen screen;
show_stats(game);
std::uint8_t x = 0;
struct HandleEvents {
void operator()(const GameState::JoyStickStateChanged &e) {
put_hex(36, 1, e.state);
}
void operator()(const GameState::TimeElapsed &e) {
put_hex(36, 0, e.us.count());
}
};
HandleEvents eventHandler;
while (true) {
const auto us_elapsed = game_clock.restart().count();
// show_stats(game);
put_hex(36, 0, us_elapsed);
std::visit(eventHandler, game.next_event());
screen.show(x++, 10, character);
if (x == 36) {
x = 0;
if (x == 11) {
x = 10;
}
increment_border_color();
}

View File

@ -264,7 +264,7 @@ struct mos6502 : ASMLine
return text;// + ':';
case ASMLine::Type::Directive:
case ASMLine::Type::Instruction: {
return fmt::format("\t{} {:15}; {}", text, op.value, comment);
return fmt::format("\t{} {:15}\t; {}", text, op.value, comment);
}
}
throw std::runtime_error("Unable to render: " + text);

View File

@ -47,6 +47,16 @@ std::string_view strip_negate(std::string_view s)
return s;
}
std::string_view strip_gs(std::string_view s)
{
const auto matcher = ctre::match<R"(gs\((.*)\))">;
if (const auto results = matcher(s); results) {
return results.get<1>();
}
return s;
}
std::string_view strip_offset(std::string_view s)
{
@ -64,8 +74,8 @@ std::string fixup_8bit_literal(const std::string &s)
if (s.starts_with("0x")) { return "#$" + s.substr(2); }
if (s.starts_with("lo8(")) { return fmt::format("#<{}", strip_lo_hi(s)); }
if (s.starts_with("hi8(")) { return fmt::format("#>{}", strip_lo_hi(s)); }
if (s.starts_with("lo8(")) { return fmt::format("#<{}", strip_gs(strip_lo_hi(s))); }
if (s.starts_with("hi8(")) { return fmt::format("#>{}", strip_gs(strip_lo_hi(s))); }
const auto is_num = std::all_of(begin(s), end(s), [](const auto c) { return (c >= '0' && c <= '9') || c == '-'; });
@ -955,8 +965,8 @@ std::vector<mos6502> run(const Personality &personality, std::istream &input, co
check_label(i.operand1.value);
check_label(i.operand2.value);
check_label(std::string{ strip_offset(strip_negate(strip_lo_hi(i.operand1.value))) });
check_label(std::string{ strip_offset(strip_negate(strip_lo_hi(i.operand2.value))) });
check_label(std::string{ strip_gs(strip_offset(strip_negate(strip_lo_hi(i.operand1.value)))) });
check_label(std::string{ strip_gs(strip_offset(strip_negate(strip_lo_hi(i.operand2.value)))) });
} else if (i.type == ASMLine::Type::Directive) {
const auto matcher = ctre::match<R"(\s*.word\s*gs\((.*)\))">;
@ -976,12 +986,18 @@ std::vector<mos6502> run(const Personality &personality, std::istream &input, co
// newl.erase(std::remove_if(newl.begin(), newl.end(), [](const auto c) { return !std::isalnum(c); }),
// std::end(newl));
// strip just first \.
if (l[0] == '.') {
result.emplace(std::make_pair(l, l.substr(1)));
} else {
result.emplace(std::make_pair(l, l));
}
const auto new_label = [](auto label) -> std::string {
if (label[0] == '.') {
label.erase(0,1);
}
for (auto &c : label) { if (c == '.') { c = '_'; }
}
return label;
};
result.emplace(std::make_pair(l, new_label(l)));
}
return result;
}();