1
0
mirror of https://github.com/lefticus/6502-cpp.git synced 2024-12-30 07:31:38 +00:00
6502-cpp/examples/16bit_counter.cpp

128 lines
2.7 KiB
C++
Raw Normal View History

2021-05-05 21:56:08 +00:00
#include <array>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <string_view>
enum Colors : uint8_t { WHITE = 0x01 };
inline volatile uint8_t &memory_loc(const uint16_t loc) {
return *reinterpret_cast<volatile uint8_t *>(loc);
}
inline void poke(const uint16_t loc, const uint8_t value) {
memory_loc(loc) = value;
}
inline std::uint8_t peek(const std::uint16_t loc) { return memory_loc(loc); }
inline void decrement_border_color() { --memory_loc(0xd020); }
inline void increment_border_color() { ++memory_loc(0xd020); }
inline bool joystick_down() {
uint8_t joystick_state = memory_loc(0xDC00);
return (joystick_state & 2) == 0;
}
void use_data(std::array<char, 1024> &data);
inline void puts(uint8_t x, uint8_t y, std::string_view str) {
const auto start = 0x400 + (y * 40 + x);
std::memcpy(const_cast<uint8_t *>(&memory_loc(start)), str.data(),
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);
}
struct Clock {
using milliseconds = std::chrono::duration<std::uint16_t, std::milli>;
// return elapsed time since last restart
[[nodiscard]] milliseconds restart() {
// stop Timer A
poke(0xDC0E, 0b00000000);
// last value
const auto previous_value = static_cast<std::uint16_t>(
peek(0xDC04) | (static_cast<uint16_t>(peek(0xDC05)) << 8));
// reset timer
poke(0xDC04, 0xFF);
poke(0xDC05, 0xFF);
// restart timer A
poke(0xDC0E, 0b00010001);
return milliseconds{0xFFFF - previous_value};
}
Clock() { [[maybe_unused]] const auto value = restart(); }
};
int main() {
// static constexpr std::array<std::uint8_t, 1000> data{0};
// std::memcpy(const_cast<uint8_t*>(&memory_loc(0x400)), data.data(),
// data.size());
/*
puts(5, 5, "hello world");
puts(10, 10, "hellooooo world");
Clock game_clock{};
*/
std::uint16_t us_elapsed = 0;
int y = 0;
while (true) {
// const auto us_elapsed = game_clock.restart().count();
for (int i = 0; i < 4; ++i) {
const auto nibble = static_cast<std::uint8_t>((us_elapsed >> (i * 4)) & 0xF);
if (nibble <= 9) {
putc(6 - i, y, nibble + 48);
} else {
putc(6 - i, y, nibble - 9);
}
}
if (y++ == 5) {
y = 0;
}
++us_elapsed;
increment_border_color();
}
/*
const auto background_color = [](Colors col) {
memory_loc(0xd021) = static_cast<uint8_t>(col);
};
background_color(Colors::WHITE);
while(true) {
if (joystick_down()) {
increment_border_color();
} else {
decrement_border_color();
}
}
*/
}