Apple1-esp/io.cpp

68 lines
1.1 KiB
C++
Raw Normal View History

2023-09-27 12:55:10 +00:00
#include <Arduino.h>
2014-11-11 17:13:25 +00:00
#include <memory.h>
2023-07-25 15:51:34 +00:00
#include <display.h>
2019-02-13 07:47:59 +00:00
#include <serialio.h>
#include <filer.h>
#include <serial_kbd.h>
#include <serial_dsp.h>
2023-09-27 10:31:46 +00:00
#include <pia.h>
2024-11-23 12:35:37 +00:00
#include <hardware.h>
2014-11-11 17:13:25 +00:00
#include "io.h"
#include "disp.h"
2018-11-18 12:48:10 +00:00
#include "config.h"
2014-11-11 17:13:25 +00:00
void io::reset() {
2024-11-23 12:35:37 +00:00
_loading = false;
_dsp.reset();
_kbd.reset();
2024-11-23 12:35:37 +00:00
_pia.reset();
_ch = 0;
2014-11-11 17:13:25 +00:00
}
2023-09-27 12:55:10 +00:00
bool io::start() {
2024-11-23 12:35:37 +00:00
hardware_interval_timer(10, [this]() { poll(); });
_pia.register_portb_write_handler([this](uint8_t b) { _dsp.write(b & 0x7f); });
_pia.register_porta_read_handler([this]() { uint8_t c = _ch; _ch = 0; return c; });
2023-09-27 12:55:10 +00:00
return files.start();
}
2014-11-14 11:45:28 +00:00
void io::load() {
2019-02-13 07:47:59 +00:00
if (files.more()) {
2014-11-14 11:45:28 +00:00
_loading = true;
2019-02-13 07:47:59 +00:00
enter(files.read());
2014-11-14 11:45:28 +00:00
}
}
2018-08-14 10:00:03 +00:00
void io::enter(uint8_t key) {
2024-11-23 12:35:37 +00:00
_pia.write_ca1(false);
_ch = key | 0x80;
_pia.write_ca1(true);
2014-11-14 11:45:28 +00:00
}
2024-11-23 12:35:37 +00:00
void io::poll() {
if (_ch)
return;
2014-11-11 17:13:25 +00:00
2014-11-24 19:37:04 +00:00
if (_loading) {
2019-02-13 07:47:59 +00:00
if (files.more())
enter(files.read());
2014-11-24 19:37:04 +00:00
else
_loading = false;
} else if (_kbd.available()) {
int c = _kbd.read();
if (c != -1)
enter(c);
2014-11-11 17:13:25 +00:00
}
}
void io::checkpoint(Stream &s) {
2024-11-23 12:35:37 +00:00
_pia.checkpoint(s);
_dsp.checkpoint(s);
2014-11-11 17:13:25 +00:00
}
void io::restore(Stream &s) {
2024-11-23 12:35:37 +00:00
_pia.restore(s);
_dsp.restore(s);
2014-11-11 17:13:25 +00:00
}