Apple1-esp/io.cpp

151 lines
2.3 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>
2023-09-27 10:31:46 +00:00
#include <pia.h>
2014-11-14 11:45:28 +00:00
#include <timed.h>
2014-11-11 17:13:25 +00:00
#include "io.h"
#include "hardware.h"
2018-11-18 12:48:10 +00:00
#include "config.h"
2014-11-11 17:13:25 +00:00
void io::reset() {
2023-07-25 15:51:34 +00:00
Display::begin(BG_COLOUR, FG_COLOUR, ORIENT);
2014-11-11 17:13:25 +00:00
clear();
2014-11-12 19:33:11 +00:00
_cy += 2;
2014-11-11 17:13:25 +00:00
2014-11-12 08:28:32 +00:00
r = c = 0;
for (int j = 0; j < ROWS; j++)
for (int i = 0; i < COLS; i++)
screen[j][i] = ' ';
_kbd.reset();
2014-11-14 11:45:28 +00:00
_loading = false;
PIA::reset();
2014-11-11 17:13:25 +00:00
}
2023-09-27 12:55:10 +00:00
static io *i;
const int TICK_FREQ = 2;
bool io::start() {
i = this;
timer_create(TICK_FREQ, io::on_tick);
return files.start();
}
void IRAM_ATTR io::on_tick() {
static int tick = 0;
tick = ++tick % 3;
i->cursor(tick < 2);
}
void io::cursor(bool on) {
draw(on? '_': ' ', c, r);
}
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) {
PIA::write_ca1(false);
PIA::write_porta_in(key + 0x80);
PIA::write_ca1(true);
2014-11-14 11:45:28 +00:00
}
2014-11-12 08:28:32 +00:00
void io::draw(char ch, int i, int j) {
if (screen[j][i] != ch) {
screen[j][i] = ch;
2014-11-12 19:33:11 +00:00
char c[2] = { ch, 0 };
2018-11-12 07:47:06 +00:00
drawString(c, i*_cx, j*_cy);
2014-11-12 08:28:32 +00:00
}
}
2018-08-14 10:00:03 +00:00
void io::display(uint8_t b) {
2014-11-11 17:13:25 +00:00
char ch = (char)b;
2014-11-12 08:28:32 +00:00
switch(ch) {
2014-11-12 19:33:11 +00:00
case 0x5f:
draw(' ', c, r);
2014-11-12 08:28:32 +00:00
if (c-- == 0) {
2014-11-11 17:13:25 +00:00
r--;
c = COLS-1;
}
break;
case 0x0d:
2014-11-12 19:33:11 +00:00
draw(' ', c, r);
2014-11-11 17:13:25 +00:00
c = 0;
r++;
break;
default:
2014-11-12 19:33:11 +00:00
if (ch >= 0x20 && ch < 0x7f) {
2014-11-12 08:28:32 +00:00
draw(ch, c, r);
2014-11-11 17:13:25 +00:00
if (++c == COLS) {
c = 0;
r++;
}
}
}
if (r == ROWS) {
// scroll
r--;
for (int j = 0; j < (ROWS-1); j++)
2014-11-12 08:28:32 +00:00
for (int i = 0; i < COLS; i++)
draw(screen[j+1][i], i, j);
for (int i = 0; i < COLS; i++)
draw(' ', i, ROWS-1);
2014-11-11 17:13:25 +00:00
}
2014-11-12 19:33:11 +00:00
draw('_', c, r);
2014-11-11 17:13:25 +00:00
}
2018-08-14 10:00:03 +00:00
void io::write_portb(uint8_t b) {
2014-11-17 20:00:40 +00:00
b &= 0x7f;
display(b);
PIA::write_portb(b);
2014-11-11 17:13:25 +00:00
}
uint8_t io::read_cra() {
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
}
return PIA::read_cra();
2014-11-11 17:13:25 +00:00
}
void io::checkpoint(Stream &s) {
PIA::checkpoint(s);
2014-11-14 13:28:48 +00:00
s.write(r);
s.write(c);
for (int j = 0; j < ROWS; j++)
for (int i = 0; i < COLS; i++)
s.write(screen[j][i]);
2014-11-11 17:13:25 +00:00
}
void io::restore(Stream &s) {
PIA::restore(s);
2014-11-14 13:28:48 +00:00
r = s.read();
c = s.read();
for (int j = 0; j < ROWS; j++)
for (int i = 0; i < COLS; i++) {
char c = s.read();
screen[j][i] = c;
draw(c, i, j);
}
draw('_', c, r);
2014-11-11 17:13:25 +00:00
}