Skip to content

Commit

Permalink
Move RAM storage out of graphics classes. (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
whscullin authored Jan 5, 2025
1 parent b7a575a commit d8073c0
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 163 deletions.
35 changes: 22 additions & 13 deletions js/apple2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Apple2 implements Restorable<State>, DebuggerContainer {

private io: Apple2IO;
private mmu: MMU | undefined;
private ram: [RAM, RAM, RAM] | undefined;
private ram: RAM[] | undefined;
private characterRom: rom;
private rom: ROM;

Expand Down Expand Up @@ -113,10 +113,26 @@ export class Apple2 implements Restorable<State>, DebuggerContainer {
this.rom = new Apple2ROM();
this.characterRom = characterRom;

this.gr = new LoresPage(this.vm, 1, this.characterRom, options.e);
this.gr2 = new LoresPage(this.vm, 2, this.characterRom, options.e);
this.hgr = new HiresPage(this.vm, 1);
this.hgr2 = new HiresPage(this.vm, 2);
this.ram = [new RAM(0x00, 0xbf)];
if (options.e) {
this.ram.push(new RAM(0x00, 0xbf));
}
this.gr = new LoresPage(
this.vm,
1,
this.ram,
this.characterRom,
options.e
);
this.gr2 = new LoresPage(
this.vm,
2,
this.ram,
this.characterRom,
options.e
);
this.hgr = new HiresPage(this.vm, 1, this.ram);
this.hgr2 = new HiresPage(this.vm, 2, this.ram);
this.io = new Apple2IO(this.cpu, this.vm);
this.tick = options.tick;

Expand All @@ -129,23 +145,16 @@ export class Apple2 implements Restorable<State>, DebuggerContainer {
this.hgr,
this.hgr2,
this.io,
this.ram,
this.rom
);
this.cpu.addPageHandler(this.mmu);
} else {
this.ram = [
new RAM(0x00, 0x03),
new RAM(0x0c, 0x1f),
new RAM(0x60, 0xbf),
];

this.cpu.addPageHandler(this.ram[0]);
this.cpu.addPageHandler(this.gr);
this.cpu.addPageHandler(this.gr2);
this.cpu.addPageHandler(this.ram[1]);
this.cpu.addPageHandler(this.hgr);
this.cpu.addPageHandler(this.hgr2);
this.cpu.addPageHandler(this.ram[2]);
this.cpu.addPageHandler(this.io);
this.cpu.addPageHandler(this.rom);
}
Expand Down
65 changes: 18 additions & 47 deletions js/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { byte, Color, memory, MemoryPages, rom } from './types';
import { allocMemPages } from './util';
import RAM from './ram';
import {
GraphicsState,
HiresPage,
LoresPage,
Region,
Expand Down Expand Up @@ -106,6 +105,7 @@ export class LoresPage2D implements LoresPage {
private _buffer: memory[] = [];
private _refreshing = false;
private _blink = false;
private _blinkInterval: ReturnType<typeof setInterval> | undefined;

private highColorTextMode = false;

Expand All @@ -115,13 +115,16 @@ export class LoresPage2D implements LoresPage {
constructor(
private vm: VideoModes,
private page: pageNo,
private readonly charset: rom,
private ram: RAM[],
private charset: rom,
private readonly e: boolean
) {
this.imageData = this.vm.context.createImageData(560, 192);
this.imageData.data.fill(0xff);
this._buffer[0] = allocMemPages(0x4);
this._buffer[1] = allocMemPages(0x4);
const start = 0x4 * this.page;
const end = start + 0x4;
this._buffer[0] = this.ram[0].getBuffer(start, end);
this._buffer[1] = this.ram[1]?.getBuffer(start, end);

this.vm.setLoresPage(page, this);
}
Expand Down Expand Up @@ -427,7 +430,9 @@ export class LoresPage2D implements LoresPage {
}

start() {
setInterval(() => this.blink(), 267);
if (!this._blinkInterval) {
this._blinkInterval = setInterval(() => this.blink(), 267);
}
return this._start();
}

Expand All @@ -443,22 +448,6 @@ export class LoresPage2D implements LoresPage {
return this._write(page, off, val, 0);
}

getState(): GraphicsState {
return {
buffer: [
new Uint8Array(this._buffer[0]),
new Uint8Array(this._buffer[1]),
],
};
}

setState(state: GraphicsState) {
this._buffer[0] = new Uint8Array(state.buffer[0]);
this._buffer[1] = new Uint8Array(state.buffer[1]);

this.refresh();
}

private rowToBase(row: number) {
const ab = (row >> 3) & 3;
const cd = (row >> 1) & 0x3;
Expand Down Expand Up @@ -526,12 +515,16 @@ export class HiresPage2D implements HiresPage {

constructor(
private vm: VideoModes,
private page: pageNo
private page: pageNo,
private ram: RAM[]
) {
this.imageData = this.vm.context.createImageData(560, 192);
this.imageData.data.fill(0xff);
this._buffer[0] = allocMemPages(0x20);
this._buffer[1] = allocMemPages(0x20);

const start = 0x20 * this.page;
const end = start + 0x20;
this._buffer[0] = this.ram[0].getBuffer(start, end);
this._buffer[1] = this.ram[1]?.getBuffer(start, end);

this.vm.setHiresPage(page, this);
}
Expand Down Expand Up @@ -873,22 +866,6 @@ export class HiresPage2D implements HiresPage {
write(page: byte, off: byte, val: byte) {
return this._write(page, off, val, 0);
}

getState(): GraphicsState {
return {
buffer: [
new Uint8Array(this._buffer[0]),
new Uint8Array(this._buffer[1]),
],
};
}

setState(state: GraphicsState) {
this._buffer[0] = new Uint8Array(state.buffer[0]);
this._buffer[1] = new Uint8Array(state.buffer[1]);

this.refresh();
}
}

export class VideoModes2D implements VideoModes {
Expand Down Expand Up @@ -1169,8 +1146,6 @@ export class VideoModes2D implements VideoModes {

getState(): VideoModesState {
return {
grs: [this._grs[0].getState(), this._grs[1].getState()],
hgrs: [this._hgrs[0].getState(), this._hgrs[1].getState()],
textMode: this.textMode,
mixedMode: this.mixedMode,
hiresMode: this.hiresMode,
Expand All @@ -1192,10 +1167,6 @@ export class VideoModes2D implements VideoModes {
this.an3State = state.an3State;
this.flag = state.flag;

this._grs[0].setState(state.grs[0]);
this._grs[1].setState(state.grs[1]);
this._hgrs[0].setState(state.hgrs[0]);
this._hgrs[1].setState(state.hgrs[1]);
this._refresh();
}

Expand Down
24 changes: 20 additions & 4 deletions js/components/debugger/FileViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEffect, useRef, useState } from 'preact/hooks';
import { Modal, ModalContent, ModalFooter } from '../Modal';

import styles from './css/FileViewer.module.scss';
import RAM from 'js/ram';

/**
* Binary and text representation of file to be previewed
Expand Down Expand Up @@ -39,8 +40,14 @@ const HiresPreview = ({ binary }: { binary: Uint8Array }) => {

if (canvasRef.current) {
const vm = new VideoModes2D(canvasRef.current, true);
const lores = new LoresPage2D(vm, 1, new Uint8Array(), false);
const hires = new HiresPage2D(vm, 1);
const lores = new LoresPage2D(
vm,
1,
[new RAM(0x4, 0x8)],
new Uint8Array(),
false
);
const hires = new HiresPage2D(vm, 1, [new RAM(0x2, 0x4)]);
vm.setLoresPage(1, lores);
vm.setHiresPage(1, hires);
vm.text(false);
Expand Down Expand Up @@ -80,8 +87,17 @@ const DoubleHiresPreview = ({ binary }: { binary: Uint8Array }) => {

if (canvasRef.current) {
const vm = new VideoModes2D(canvasRef.current, true);
const lores = new LoresPage2D(vm, 1, new Uint8Array(), false);
const hires = new HiresPage2D(vm, 1);
const lores = new LoresPage2D(
vm,
1,
[new RAM(0x2, 0x4), new RAM(0x2, 0x4)],
new Uint8Array(),
false
);
const hires = new HiresPage2D(vm, 1, [
new RAM(0x02, 0x04),
new RAM(0x02, 0x04),
]);
vm.setLoresPage(1, lores);
vm.setHiresPage(1, hires);
vm.text(false);
Expand Down
66 changes: 19 additions & 47 deletions js/gl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { byte, Color, memory, MemoryPages, rom } from './types';
import { allocMemPages } from './util';

import { screenEmu } from 'apple2shader';

import {
GraphicsState,
HiresPage,
LoresPage,
Region,
Expand All @@ -13,6 +11,7 @@ import {
bank,
pageNo,
} from './videomodes';
import RAM from './ram';

// Color constants
const whiteCol: Color = [255, 255, 255];
Expand All @@ -39,20 +38,25 @@ export class LoresPageGL implements LoresPage {
private _buffer: memory[] = [];
private _refreshing = false;
private _blink = false;
private _blinkInterval: ReturnType<typeof setInterval> | undefined;

dirty: Region = { ...notDirty };
imageData: ImageData;

constructor(
private vm: VideoModes,
private page: pageNo,
private readonly charset: rom,
private ram: RAM[],
private charset: rom,
private readonly e: boolean
) {
this.imageData = this.vm.context.createImageData(560, 192);
this.imageData.data.fill(0xff);
this._buffer[0] = allocMemPages(0x4);
this._buffer[1] = allocMemPages(0x4);

const start = 0x4 * this.page;
const end = start + 0x4;
this._buffer[0] = this.ram[0].getBuffer(start, end);
this._buffer[1] = this.ram[1]?.getBuffer(start, end);

this.vm.setLoresPage(page, this);
}
Expand Down Expand Up @@ -293,7 +297,9 @@ export class LoresPageGL implements LoresPage {
}

start() {
setInterval(() => this.blink(), 267);
if (!this._blinkInterval) {
this._blinkInterval = setInterval(() => this.blink(), 267);
}
return this._start();
}

Expand All @@ -309,22 +315,6 @@ export class LoresPageGL implements LoresPage {
return this._write(page, off, val, 0);
}

getState(): GraphicsState {
return {
buffer: [
new Uint8Array(this._buffer[0]),
new Uint8Array(this._buffer[1]),
],
};
}

setState(state: GraphicsState) {
this._buffer[0] = new Uint8Array(state.buffer[0]);
this._buffer[1] = new Uint8Array(state.buffer[1]);

this.refresh();
}

private rowToBase(row: number) {
const ab = (row >> 3) & 3;
const cd = (row >> 1) & 0x3;
Expand Down Expand Up @@ -406,12 +396,16 @@ export class HiresPageGL implements HiresPage {

constructor(
private vm: VideoModes,
private page: pageNo
private page: pageNo,
private ram: RAM[]
) {
this.imageData = this.vm.context.createImageData(560, 192);
this.imageData.data.fill(0xff);
this._buffer[0] = allocMemPages(0x20);
this._buffer[1] = allocMemPages(0x20);

const start = 0x20 * this.page;
const end = start + 0x20;
this._buffer[0] = this.ram[0].getBuffer(start, end);
this._buffer[1] = this.ram[1]?.getBuffer(start, end);

this.vm.setHiresPage(page, this);
}
Expand Down Expand Up @@ -573,22 +567,6 @@ export class HiresPageGL implements HiresPage {
write(page: byte, off: byte, val: byte) {
return this._write(page, off, val, 0);
}

getState(): GraphicsState {
return {
buffer: [
new Uint8Array(this._buffer[0]),
new Uint8Array(this._buffer[1]),
],
};
}

setState(state: GraphicsState) {
this._buffer[0] = new Uint8Array(state.buffer[0]);
this._buffer[1] = new Uint8Array(state.buffer[1]);

this.refresh();
}
}

export class VideoModesGL implements VideoModes {
Expand Down Expand Up @@ -904,8 +882,6 @@ export class VideoModesGL implements VideoModes {

getState(): VideoModesState {
return {
grs: [this._grs[0].getState(), this._grs[1].getState()],
hgrs: [this._hgrs[0].getState(), this._hgrs[1].getState()],
textMode: this.textMode,
mixedMode: this.mixedMode,
hiresMode: this.hiresMode,
Expand All @@ -926,10 +902,6 @@ export class VideoModesGL implements VideoModes {
this.altCharMode = state.altCharMode;
this.an3State = state.an3State;

this._grs[0].setState(state.grs[0]);
this._grs[1].setState(state.grs[1]);
this._hgrs[0].setState(state.hgrs[0]);
this._hgrs[1].setState(state.hgrs[1]);
this._refresh();
}

Expand Down
Loading

0 comments on commit d8073c0

Please sign in to comment.