factor out pia

This commit is contained in:
Stephen Crane 2014-11-17 20:00:40 +00:00
parent 2256502943
commit 82c51380f1
6 changed files with 127 additions and 108 deletions

View File

@ -1,2 +0,0 @@
- break out PIA abstraction
- checkpoint and restore images (i.e., startrek)

View File

@ -7,6 +7,7 @@
#include "roms/basic.h"
#include "roms/monitor.h"
#include "pia.h"
#include "io.h"
#include "config.h"

117
io.cpp
View File

@ -5,13 +5,11 @@
#include <keyboard.h>
#include <timed.h>
#include "pia.h"
#include "io.h"
#include "config.h"
#include "hardware.h"
static byte dsp_cr, dsp, kbd_cr, kbd;
static unsigned kbd_int, dsp_out;
#define ROWS 24
#define COLS 40
static unsigned r, c;
@ -28,10 +26,7 @@ void io::reset() {
screen[j][i] = ' ';
_loading = false;
// PIA state
dsp_cr = kbd_cr = 0;
kbd_int = dsp_out = 0;
pia::reset();
}
void io::load() {
@ -81,14 +76,13 @@ static const byte shiftmap[] = {
};
void io::down(byte scan) {
kbd = 0;
set_porta(0);
if (isshift(scan))
_shift = true;
}
void io::enter(byte key) {
kbd = key + 0x80;
kbd_cr = 0xa7;
set_porta(key + 0x80);
}
void io::up(byte scan) {
@ -143,95 +137,27 @@ void io::display(byte b) {
draw('_', c, r);
}
void io::operator=(byte b) {
Serial.print(">");
Serial.print(_acc, 16);
Serial.print(" ");
Serial.println(b, 16);
switch(_acc % 4) {
case 0:
kbd = b;
break;
case 1:
if (!kbd_int && b >= 0x80)
kbd_int = 1;
else
kbd_cr = b;
break;
case 2:
if (b >= 0x80)
b -= 0x80;
display(b);
dsp = b;
break;
case 3:
if (!dsp_out && dsp_cr >= 0x80)
dsp_out = 1;
else
dsp_cr = b;
break;
}
void io::write_portb(byte b) {
b &= 0x7f;
display(b);
pia::write_portb(b);
}
io::operator byte() {
switch (_acc % 4) {
case 0:
/*
Serial.print("<");
Serial.print(_acc, 16);
Serial.print(" ");
Serial.println(kbd, 16);
*/
return kbd;
case 1:
if (kbd_int && kbd_cr >= 0x80) {
kbd_cr = 0;
/*
Serial.print("<");
Serial.print(_acc, 16);
Serial.print(" ");
Serial.println(0xa7, 16);
*/
if (_loading) {
if (tape.more())
enter(tape.read());
else
_loading = false;
}
return 0xa7;
byte io::read_porta_cr() {
byte b = pia::read_porta_cr();
if (b == 0xa7) {
if (_loading) {
if (tape.more())
enter(tape.read());
else
_loading = false;
}
//Serial.println(kbd_cr, 16);
return kbd_cr;
case 2:
/*
Serial.print("<");
Serial.print(_acc, 16);
Serial.print(" ");
Serial.println(dsp, 16);
*/
return dsp;
case 3:
/*
Serial.print("<");
Serial.print(_acc, 16);
Serial.print(" ");
Serial.println(dsp_cr, 16);
*/
return dsp_cr;
}
return 0;
return b;
}
void io::checkpoint(Stream &s) {
s.write(dsp_cr);
s.write(dsp);
s.write(kbd_cr);
s.write(kbd);
s.write(kbd_int);
s.write(dsp_out);
pia::checkpoint(s);
s.write(r);
s.write(c);
for (int j = 0; j < ROWS; j++)
@ -240,12 +166,7 @@ void io::checkpoint(Stream &s) {
}
void io::restore(Stream &s) {
dsp_cr = s.read();
dsp = s.read();
kbd_cr = s.read();
kbd = s.read();
kbd_int = s.read();
dsp_out = s.read();
pia::restore(s);
r = s.read();
c = s.read();
for (int j = 0; j < ROWS; j++)

15
io.h
View File

@ -1,19 +1,18 @@
#ifndef _IO_H
#define _IO_H
class io: public UTFTDisplay, Keyboard {
// http://mamedev.org/source/src/mess/machine/apple1.c.html
class io: public UTFTDisplay, Keyboard, public pia {
public:
io(): UTFTDisplay(256) {}
void operator=(byte);
operator byte();
virtual void reset();
virtual void down(byte scan);
virtual void up(byte scan);
void checkpoint(Stream &);
void restore(Stream &);
virtual void checkpoint(Stream &);
virtual void restore(Stream &);
virtual void write_portb(byte);
virtual byte read_porta_cr();
void load();
sdtape tape;

49
pia.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <Energia.h>
#include <memory.h>
#include "pia.h"
void pia::operator=(byte b) {
switch(_acc % 4) {
case 0:
write_porta(b);
break;
case 1:
write_porta_cr(b);
break;
case 2:
write_portb(b);
break;
case 3:
write_portb_cr(b);
break;
}
}
pia::operator byte() {
switch (_acc % 4) {
case 0:
return read_porta();
case 1:
return read_porta_cr();
case 2:
return read_portb();
case 3:
return read_portb_cr();
}
return 0xff;
}
void pia::checkpoint(Stream &s) {
s.write(portb_cr);
s.write(portb);
s.write(porta_cr);
s.write(porta);
}
void pia::restore(Stream &s) {
portb_cr = s.read();
portb = s.read();
porta_cr = s.read();
porta = s.read();
}

51
pia.h Normal file
View File

@ -0,0 +1,51 @@
#ifndef __PIA_H__
#define __PIA_H__
class pia: public Memory::Device {
public:
pia(): Memory::Device(256), portb_cr(0), porta_cr(0) {}
virtual void reset() { portb_cr = porta_cr = 0; }
void operator=(byte);
operator byte();
void checkpoint(Stream &);
void restore(Stream &);
protected:
// write to the "external" side of the port
void set_porta(byte b) {
porta = b;
if (b & 0x80)
porta_cr = 0xa7;
}
// "device-side" operations (called from memory interface)
byte read_porta() { return porta; }
virtual byte read_porta_cr() {
if (porta_cr & 0x80) {
porta_cr = 0;
return 0xa7;
}
return porta_cr;
}
byte read_portb() { return portb; }
byte read_portb_cr() { return portb_cr; }
void write_porta(byte b) { porta = b; }
void write_porta_cr(byte b) {
if (!(porta_cr & 0x80) && b >= 0x80)
porta_cr |= 0x80;
else
porta_cr = b;
}
virtual void write_portb(byte b) { portb = b; }
void write_portb_cr(byte b) {
if (portb_cr < 0x80)
portb_cr = b;
}
private:
byte portb_cr, portb, porta_cr, porta;
};
#endif