2021-03-13 21:18:32 +00:00
|
|
|
import RAM, { RAMState } from '../ram';
|
|
|
|
import { debug } from '../util';
|
|
|
|
import { Card, Memory, byte, Restorable } from '../types';
|
|
|
|
|
|
|
|
export interface LanguageCardState {
|
|
|
|
bank1: RAMState;
|
|
|
|
bank2: RAMState;
|
|
|
|
ram: RAMState;
|
|
|
|
|
|
|
|
readbsr: boolean;
|
|
|
|
writebsr: boolean;
|
|
|
|
bsr2: boolean;
|
|
|
|
prewrite: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class LanguageCard implements Card, Restorable<LanguageCardState> {
|
|
|
|
private bank1: RAM;
|
|
|
|
private bank2: RAM;
|
|
|
|
private ram: RAM;
|
|
|
|
|
2022-07-14 03:34:50 +00:00
|
|
|
private _readbsr = false;
|
|
|
|
private _writebsr = false;
|
|
|
|
private _bsr2 = false;
|
|
|
|
private _prewrite = false;
|
2021-03-13 21:18:32 +00:00
|
|
|
|
|
|
|
private read1: Memory;
|
|
|
|
private read2: Memory;
|
|
|
|
|
|
|
|
private write1: Memory;
|
|
|
|
private write2: Memory;
|
|
|
|
|
|
|
|
constructor(private rom: Memory) {
|
|
|
|
debug('Language card');
|
|
|
|
|
|
|
|
this.bank1 = new RAM(0xd0, 0xdf);
|
|
|
|
this.bank2 = new RAM(0xd0, 0xdf);
|
|
|
|
this.ram = new RAM(0xe0, 0xff);
|
|
|
|
|
|
|
|
this.write1 = this.rom;
|
|
|
|
this.write2 = this.rom;
|
|
|
|
|
|
|
|
this.read1 = this.rom;
|
|
|
|
this.read2 = this.rom;
|
|
|
|
}
|
|
|
|
|
2022-05-18 02:08:28 +00:00
|
|
|
private debug(..._args: unknown[]) {
|
2021-03-13 21:22:45 +00:00
|
|
|
// debug.apply(null, args);
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private updateBanks() {
|
2022-07-14 03:34:50 +00:00
|
|
|
if (this._readbsr) {
|
|
|
|
this.read1 = this._bsr2 ? this.bank2 : this.bank1;
|
2021-03-13 21:18:32 +00:00
|
|
|
this.read2 = this.ram;
|
|
|
|
} else {
|
|
|
|
this.read1 = this.rom;
|
|
|
|
this.read2 = this.rom;
|
|
|
|
}
|
|
|
|
|
2022-07-14 03:34:50 +00:00
|
|
|
if (this._writebsr) {
|
|
|
|
this.write1 = this._bsr2 ? this.bank2 : this.bank1;
|
2021-03-13 21:18:32 +00:00
|
|
|
this.write2 = this.ram;
|
|
|
|
} else {
|
|
|
|
this.write1 = this.rom;
|
|
|
|
this.write2 = this.rom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bank 2
|
|
|
|
// READBSR2: 0x80
|
|
|
|
// WRITEBSR2: 0x81
|
|
|
|
// OFFBSR2: 0x82
|
|
|
|
// READWRBSR2: 0x83
|
|
|
|
|
|
|
|
// Bank 1
|
|
|
|
// READBSR1: 0x88
|
|
|
|
// WRITEBSR1: 0x89
|
|
|
|
// OFFBSR1: 0x8a
|
|
|
|
// READWRBSR1: 0x8b
|
|
|
|
|
|
|
|
private access(off: byte, val?: byte) {
|
|
|
|
const readMode = val === undefined;
|
|
|
|
const result = readMode ? 0 : undefined;
|
|
|
|
|
|
|
|
const writeSwitch = off & 0x01;
|
|
|
|
const offSwitch = off & 0x02;
|
|
|
|
const bank1Switch = off & 0x08;
|
|
|
|
|
|
|
|
let bankStr;
|
|
|
|
let rwStr;
|
|
|
|
|
|
|
|
if (writeSwitch) { // $C081, $C083, $C089, $C08B
|
|
|
|
if (readMode) {
|
2022-07-14 03:34:50 +00:00
|
|
|
this._writebsr = this._prewrite;
|
2021-03-13 21:18:32 +00:00
|
|
|
}
|
2022-07-14 03:34:50 +00:00
|
|
|
this._prewrite = readMode;
|
2021-03-13 21:18:32 +00:00
|
|
|
|
|
|
|
if (offSwitch) { // $C083, $C08B
|
2022-07-14 03:34:50 +00:00
|
|
|
this._readbsr = true;
|
2021-03-13 21:18:32 +00:00
|
|
|
rwStr = 'Read/Write';
|
|
|
|
} else { // $C081, $C089
|
2022-07-14 03:34:50 +00:00
|
|
|
this._readbsr = false;
|
2021-03-13 21:18:32 +00:00
|
|
|
rwStr = 'Write';
|
|
|
|
}
|
|
|
|
} else { // $C080, $C082, $C088, $C08A
|
2022-07-14 03:34:50 +00:00
|
|
|
this._writebsr = false;
|
|
|
|
this._prewrite = false;
|
2021-03-13 21:18:32 +00:00
|
|
|
|
|
|
|
if (offSwitch) { // $C082, $C08A
|
2022-07-14 03:34:50 +00:00
|
|
|
this._readbsr = false;
|
2021-03-13 21:18:32 +00:00
|
|
|
rwStr = 'Off';
|
|
|
|
} else { // $C080, $C088
|
2022-07-14 03:34:50 +00:00
|
|
|
this._readbsr = true;
|
2021-03-13 21:18:32 +00:00
|
|
|
rwStr = 'Read';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bank1Switch) { // C08[8-C]
|
2022-07-14 03:34:50 +00:00
|
|
|
this._bsr2 = false;
|
2021-03-13 21:18:32 +00:00
|
|
|
bankStr = 'Bank 1';
|
|
|
|
} else { // C08[0-3]
|
2022-07-14 03:34:50 +00:00
|
|
|
this._bsr2 = true;
|
2021-03-13 21:18:32 +00:00
|
|
|
bankStr = 'Bank 2';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.debug(bankStr, rwStr);
|
|
|
|
this.updateBanks();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
return 0xd0;
|
|
|
|
}
|
|
|
|
|
|
|
|
end() {
|
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
ioSwitch(off: byte, val?: byte) {
|
|
|
|
return this.access(off, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
read(page: byte, off: byte): byte {
|
|
|
|
let result: number = 0;
|
|
|
|
if (page < 0xe0) {
|
|
|
|
result = this.read1.read(page, off);
|
|
|
|
} else {
|
|
|
|
result = this.read2.read(page, off);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
write(page: byte, off: byte, val: byte) {
|
|
|
|
if (page < 0xe0) {
|
|
|
|
this.write1.write(page, off, val);
|
|
|
|
} else {
|
|
|
|
this.write2.write(page, off, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 03:34:50 +00:00
|
|
|
public get bsr2() {
|
|
|
|
return this._bsr2;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get readbsr() {
|
|
|
|
return this._readbsr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get writebsr() {
|
|
|
|
return this._writebsr;
|
|
|
|
}
|
|
|
|
|
2021-03-13 21:18:32 +00:00
|
|
|
getState() {
|
|
|
|
return {
|
|
|
|
readbsr: this.readbsr,
|
|
|
|
writebsr: this.writebsr,
|
|
|
|
bsr2: this.bsr2,
|
2022-07-14 03:34:50 +00:00
|
|
|
prewrite: this._prewrite,
|
2021-03-13 21:18:32 +00:00
|
|
|
ram: this.ram.getState(),
|
|
|
|
bank1: this.bank1.getState(),
|
|
|
|
bank2: this.bank2.getState()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(state: LanguageCardState) {
|
2022-07-14 03:34:50 +00:00
|
|
|
this._readbsr = state.readbsr;
|
|
|
|
this._writebsr = state.writebsr;
|
|
|
|
this._bsr2 = state.bsr2;
|
|
|
|
this._prewrite = state.prewrite;
|
2021-03-13 21:18:32 +00:00
|
|
|
this.ram.setState(state.ram);
|
|
|
|
this.bank1.setState(state.bank1);
|
|
|
|
this.bank2.setState(state.bank2);
|
|
|
|
this.updateBanks();
|
|
|
|
}
|
|
|
|
}
|