From 87b60d4eb41b1526b025bf0ae7acab5fd362c112 Mon Sep 17 00:00:00 2001 From: Will Scullin Date: Sat, 6 Mar 2021 15:04:13 -0800 Subject: [PATCH] Type restructuring --- js/apple2.ts | 8 ++++---- js/apple2io.ts | 6 +++--- js/canvas.ts | 13 ++++++------- js/cards/disk2.ts | 4 ++-- js/cpu6502.ts | 22 ++++++---------------- js/gl.ts | 14 +++++++------- js/mmu.ts | 5 +++-- js/roms/rom.ts | 5 ++--- js/types.ts | 13 ++++++++----- js/videomodes.ts | 8 ++++---- test/roms/6502test.ts | 5 ++--- test/roms/65C02test.ts | 5 ++--- 12 files changed, 49 insertions(+), 59 deletions(-) diff --git a/js/apple2.ts b/js/apple2.ts index b541786..8a2072a 100644 --- a/js/apple2.ts +++ b/js/apple2.ts @@ -23,11 +23,11 @@ import RAM, { RAMState } from './ram'; import { debug } from './util'; import SYMBOLS from './symbols'; -import { Restorable, memory } from './types'; +import { Restorable, rom } from './types'; import { processGamepad } from './ui/gamepad'; -interface Options { - characterRom: memory, +export interface Apple2Options { + characterRom: rom, enhanced: boolean, e: boolean, gl: boolean, @@ -73,7 +73,7 @@ export class Apple2 implements Restorable { renderedFrames: 0 }; - constructor(options: Options) { + constructor(options: Apple2Options) { const LoresPage = options.gl ? LoresPageGL : LoresPage2D; const HiresPage = options.gl ? HiresPageGL : HiresPage2D; const VideoModes = options.gl ? VideoModesGL : VideoModes2D; diff --git a/js/apple2io.ts b/js/apple2io.ts index ff6c02b..c9b127f 100644 --- a/js/apple2io.ts +++ b/js/apple2io.ts @@ -9,8 +9,8 @@ * implied warranty. */ -import CPU6502, { PageHandler } from './cpu6502'; -import { Card, Memory, TapeData, byte, Restorable } from './types'; +import CPU6502 from './cpu6502'; +import { Card, Memory, MemoryPages, TapeData, byte, Restorable } from './types'; import { debug } from './util'; type slot = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7; @@ -66,7 +66,7 @@ const LOC = { ACCEL: 0x74, // CPU Speed control }; -export default class Apple2IO implements PageHandler, Restorable { +export default class Apple2IO implements MemoryPages, Restorable { private _slot: Array = new Array(7).fill(null); private _auxRom: Memory | null = null; diff --git a/js/canvas.ts b/js/canvas.ts index 7a27cf4..186348a 100644 --- a/js/canvas.ts +++ b/js/canvas.ts @@ -9,7 +9,7 @@ * implied warranty. */ -import { byte, memory, Memory } from './types'; +import { byte, memory, MemoryPages, rom } from './types'; import { allocMemPages } from './util'; import { Color, @@ -152,7 +152,7 @@ export class LoresPage2D implements LoresPage { imageData: ImageData; constructor(private page: number, - private readonly charset: memory, + private readonly charset: rom, private readonly e: boolean) { this.imageData = new ImageData(560, 384); for (let idx = 0; idx < 560 * 384 * 4; idx++) { @@ -184,8 +184,7 @@ export class LoresPage2D implements LoresPage { data[nextOff + 2] = c2; } - - bank0(): Memory { + bank0(): MemoryPages { return { start: () => this._start(), end: () => this._end(), @@ -194,7 +193,7 @@ export class LoresPage2D implements LoresPage { }; } - bank1(): Memory { + bank1(): MemoryPages { return { start: () => this._start(), end: () => this._end(), @@ -592,7 +591,7 @@ export class HiresPage2D implements HiresPage { data[nextOff + 2] = data[nextOff + 6] = data[nextOff + 10] = data[nextOff + 14] = c2; } - bank0(): Memory { + bank0(): MemoryPages { return { start: () => this._start(), end: () => this._end(), @@ -601,7 +600,7 @@ export class HiresPage2D implements HiresPage { }; } - bank1(): Memory { + bank1(): MemoryPages { return { start: () => this._start(), end: () => this._end(), diff --git a/js/cards/disk2.ts b/js/cards/disk2.ts index 2059651..16e269b 100644 --- a/js/cards/disk2.ts +++ b/js/cards/disk2.ts @@ -10,7 +10,7 @@ */ import { base64_decode, base64_encode} from '../base64'; -import { bit, byte, DiskFormat, MemberOf, memory, nibble, rom } from '../types'; +import { bit, byte, Card, DiskFormat, MemberOf, memory, nibble, rom } from '../types'; import { debug, toHex } from '../util'; import { Disk, jsonDecode, jsonEncode, readSector } from '../formats/format_utils'; @@ -261,7 +261,7 @@ function setDriveState(state: DriveState) { /** * Emulates the 16-sector and 13-sector versions of the Disk ][ drive and controller. */ -export default class DiskII { +export default class DiskII implements Card { private drives: Drive[] = [ { // Drive 1 diff --git a/js/cpu6502.ts b/js/cpu6502.ts index 1e06b61..113f239 100644 --- a/js/cpu6502.ts +++ b/js/cpu6502.ts @@ -10,7 +10,7 @@ * implied warranty. */ -import { byte, word } from './types'; +import { Memory, MemoryPages, byte, word } from './types'; import { debug, toHex } from './util'; type symbols = { [key: number]: string }; @@ -95,25 +95,15 @@ const loc = { BRK: 0xFFFE }; -export interface Page { - read(page: byte, offset: byte): byte; - write(page: byte, offset: byte, value: byte): void; -} - -export interface PageHandler extends Page { - start(): byte; - end(): byte; -} - -interface ResettablePageHandler extends PageHandler { +interface ResettablePageHandler extends MemoryPages { reset(): void; } -function isResettablePageHandler(pageHandler: PageHandler | ResettablePageHandler): pageHandler is ResettablePageHandler { +function isResettablePageHandler(pageHandler: MemoryPages | ResettablePageHandler): pageHandler is ResettablePageHandler { return (pageHandler as ResettablePageHandler).reset !== undefined; } -const BLANK_PAGE: Page = { +const BLANK_PAGE: Memory = { read: function () { return 0; }, write: function () { } }; @@ -158,7 +148,7 @@ export default class CPU6502 { private yr = 0; // Y Register private sp = 0xff; // Stack Pointer - private memPages: Page[] = new Array(0x100); + private memPages: Memory[] = new Array(0x100); private resetHandlers: ResettablePageHandler[] = []; private cycles = 0; private sync = false; @@ -1129,7 +1119,7 @@ export default class CPU6502 { } } - public addPageHandler(pho: (PageHandler | ResettablePageHandler) & Page) { + public addPageHandler(pho: (MemoryPages | ResettablePageHandler)) { for (let idx = pho.start(); idx <= pho.end(); idx++) { this.memPages[idx] = pho; } diff --git a/js/gl.ts b/js/gl.ts index c2eb598..310a70b 100644 --- a/js/gl.ts +++ b/js/gl.ts @@ -9,7 +9,7 @@ * implied warranty. */ -import { byte, memory, Memory, Restorable } from './types'; +import { byte, memory, MemoryPages, rom } from './types'; import { allocMemPages } from './util'; import { screenEmu } from 'apple2shader'; @@ -92,7 +92,7 @@ export class LoresPageGL implements LoresPage { imageData: ImageData; constructor(private page: number, - private readonly charset: memory, + private readonly charset: rom, private readonly e: boolean) { this.imageData = new ImageData(560, 192); for (let idx = 0; idx < 560 * 192 * 4; idx++) { @@ -116,7 +116,7 @@ export class LoresPageGL implements LoresPage { data[off + 2] = c2; } - bank0(): Memory { + bank0(): MemoryPages { return { start: () => this._start(), end: () => this._end(), @@ -125,7 +125,7 @@ export class LoresPageGL implements LoresPage { }; } - bank1(): Memory { + bank1(): MemoryPages { return { start: () => this._start(), end: () => this._end(), @@ -402,7 +402,7 @@ export class LoresPageGL implements LoresPage { * ***************************************************************************/ -export class HiresPageGL implements Memory, Restorable { +export class HiresPageGL implements HiresPage { private _buffer: memory[] = []; private _refreshing = false; @@ -442,7 +442,7 @@ export class HiresPageGL implements Memory, Restorable { data[off + 2] = c2; } - bank0(): Memory { + bank0(): MemoryPages { return { start: () => this._start(), end: () => this._end(), @@ -451,7 +451,7 @@ export class HiresPageGL implements Memory, Restorable { }; } - bank1(): Memory { + bank1(): MemoryPages { return { start: () => this._start(), end: () => this._end(), diff --git a/js/mmu.ts b/js/mmu.ts index 08ae814..e75c245 100644 --- a/js/mmu.ts +++ b/js/mmu.ts @@ -9,6 +9,7 @@ * implied warranty. */ +import { MemoryPages } from './types'; import CPU6502 from './cpu6502'; import RAM, { RAMState } from './ram'; import ROM, { ROMState } from './roms/rom'; @@ -94,7 +95,7 @@ const LOC = { READWRBSR1: 0x8b, }; -class Switches implements Memory { +class Switches implements MemoryPages { constructor(private mmu: MMU) {} start() { @@ -114,7 +115,7 @@ class Switches implements Memory { } } -class AuxRom implements Memory { +class AuxRom implements MemoryPages { constructor( private readonly mmu: MMU, private readonly rom: ROM) { } diff --git a/js/roms/rom.ts b/js/roms/rom.ts index c027532..9be7841 100644 --- a/js/roms/rom.ts +++ b/js/roms/rom.ts @@ -1,9 +1,8 @@ -import { PageHandler } from '../cpu6502'; -import { Restorable, byte, rom } from '../types'; +import { MemoryPages, Restorable, byte, rom } from '../types'; export type ROMState = null; -export default class ROM implements PageHandler, Restorable { +export default class ROM implements MemoryPages, Restorable { constructor( private readonly startPage: byte, diff --git a/js/types.ts b/js/types.ts index fe59544..fee86a5 100644 --- a/js/types.ts +++ b/js/types.ts @@ -29,18 +29,21 @@ export type memory = Uint8Array; /** A raw region of memory. */ export type rom = ReadonlyUint8Array; -/** A mapped region of memory. */ export interface Memory { - /** Start page. */ - start(): byte; - /** End page, inclusive. */ - end(): byte; /** Read a byte. */ read(page: byte, offset: byte): byte; /** Write a byte. */ write(page: byte, offset: byte, value: byte): void; } +/** A mapped region of memory. */ +export interface MemoryPages extends Memory { + /** Start page. */ + start(): byte; + /** End page, inclusive. */ + end(): byte; +} + /* An interface card */ export interface Card extends Memory, Restorable { /* Reset the card */ diff --git a/js/videomodes.ts b/js/videomodes.ts index f09bffc..cd6ff3e 100644 --- a/js/videomodes.ts +++ b/js/videomodes.ts @@ -1,4 +1,4 @@ -import { Memory, Restorable, byte, memory } from './types'; +import { MemoryPages, Restorable, byte, memory } from './types'; export type bank = 0 | 1; export type pageNo = 1 | 2; @@ -34,12 +34,12 @@ export interface VideoModesState { an3: boolean, } -export interface VideoPage extends Memory, Restorable { +export interface VideoPage extends MemoryPages, Restorable { imageData: ImageData dirty: Region; - bank0(): Memory - bank1(): Memory + bank0(): MemoryPages + bank1(): MemoryPages mono: (on: boolean) => void refresh: () => void diff --git a/test/roms/6502test.ts b/test/roms/6502test.ts index 9292ee2..bbe99b4 100644 --- a/test/roms/6502test.ts +++ b/test/roms/6502test.ts @@ -2,10 +2,9 @@ import fs from 'fs'; import path from 'path'; -import { PageHandler } from '../../js/cpu6502'; -import { byte } from '../../js/types'; +import { MemoryPages, byte } from '../../js/types'; -export default class Test6502 implements PageHandler { +export default class Test6502 implements MemoryPages { private data: Buffer constructor() { diff --git a/test/roms/65C02test.ts b/test/roms/65C02test.ts index f552d31..9eca9c3 100644 --- a/test/roms/65C02test.ts +++ b/test/roms/65C02test.ts @@ -2,10 +2,9 @@ import fs from 'fs'; import path from 'path'; -import { PageHandler } from '../../js/cpu6502'; -import { byte } from '../../js/types'; +import { MemoryPages, byte } from '../../js/types'; -export default class Test65C02 implements PageHandler { +export default class Test65C02 implements MemoryPages { private data: Buffer constructor() {