Skip to content

Commit

Permalink
CommaVideo scheme support.
Browse files Browse the repository at this point in the history
  • Loading branch information
DirtyHairy committed Jun 7, 2019
1 parent 923151a commit 26c797f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
106 changes: 106 additions & 0 deletions src/machine/stella/cartridge/CartridgeCV.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* This file is part of 6502.ts, an emulator for 6502 based systems built
* in Typescript.
*
* Copyright (C) 2014 - 2018 Christian Speckner & contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import AbstractCartridge from './AbstractCartridge';
import CartridgeInfo from './CartridgeInfo';
import RngGeneratorInterface from '../../../tools/rng/GeneratorInterface';
import Bus from '../Bus';
import { BufferInterface, searchForSignatures } from './util';

class CartridgeCV extends AbstractCartridge {
constructor(buffer: { [i: number]: number; length: number }) {
super();

if (buffer.length !== 0x0800) {
throw new Error(`buffer is not a 2k cartridge image: wrong length ${buffer.length}`);
}

for (let i = 0; i < 0x0800; i++) {
this._rom[i] = buffer[i];
}
}

static matchesBuffer(buffer: BufferInterface): boolean {
// Signatures shamelessly stolen from Stella
const signatureCounts = searchForSignatures(buffer, [[0x9d, 0xff, 0xf3], [0x99, 0x00, 0xf4]]);

return signatureCounts[0] > 0 || signatureCounts[1] > 0;
}

setBus(bus: Bus): this {
this._bus = bus;

return this;
}

randomize(rng: RngGeneratorInterface): void {
for (let i = 0; i < 0x0400; i++) {
this._ram[i] = rng.int(0xff);
}
}

read(address: number): number {
address &= 0x0fff;

if (address < 0x0400) {
return this._ram[address];
}

if (address < 0x0800) {
return (this._ram[address & 0x03ff] = this._bus.getLastDataBusValue());
}

return this._rom[address & 0x07ff];
}

write(address: number, value: number): void {
address &= 0x0fff;

if (address >= 0x0400 && address < 0x0800) {
this._ram[address & 0x03ff] = value;
}
}

peek(address: number): number {
address &= 0x0fff;

if (address < 0x0400) {
return this._ram[address];
}

if (address < 0x0800) {
return 0;
}

return this._rom[address & 0x07ff];
}

getType(): CartridgeInfo.CartridgeType {
return CartridgeInfo.CartridgeType.bankswitch_2k_cv;
}

private _rom = new Uint8Array(0x0800);
private _ram = new Uint8Array(0x0400);

private _bus: Bus;
}

export default CartridgeCV;
14 changes: 13 additions & 1 deletion src/machine/stella/cartridge/CartridgeDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ import CartridgeDPCPlus from './CartridgeDPCPlus';
import CartridgeCDF from './CartridgeCDF';
import Cartridge8040 from './Cartridge0840';
import * as cartridgeUtil from './util';
import CartridgeCV from './CartridgeCV';

class CartridgeDetector {
detectCartridgeType(buffer: cartridgeUtil.BufferInterface): CartridgeInfo.CartridgeType {
if (buffer.length % 8448 === 0) {
return CartridgeInfo.CartridgeType.bankswitch_supercharger;
}

if (buffer.length <= 0x0800) {
if (buffer.length < 0x0800) {
return CartridgeInfo.CartridgeType.vanilla_2k;
}

Expand All @@ -49,6 +50,9 @@ class CartridgeDetector {
}

switch (buffer.length) {
case 0x0800:
return this._detect2k(buffer);

case 0x1000:
return CartridgeInfo.CartridgeType.vanilla_4k;

Expand Down Expand Up @@ -78,6 +82,14 @@ class CartridgeDetector {
}
}

private _detect2k(buffer: cartridgeUtil.BufferInterface): CartridgeInfo.CartridgeType {
if (CartridgeCV.matchesBuffer(buffer)) {
return CartridgeInfo.CartridgeType.bankswitch_2k_cv;
}

return CartridgeInfo.CartridgeType.vanilla_2k;
}

private _detect8k(buffer: cartridgeUtil.BufferInterface): CartridgeInfo.CartridgeType {
const f8Matches = CartridgeF8.matchesBuffer(buffer);

Expand Down
4 changes: 4 additions & 0 deletions src/machine/stella/cartridge/CartridgeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import Cartridge2k from './Cartridge2k';
import Cartridge4k from './Cartridge4k';
import CartridgeCV from './CartridgeCV';
import CartridgeF8 from './CartridgeF8';
import CartridgeF6 from './CartridgeF6';
import CartridgeE0 from './CartridgeE0';
Expand Down Expand Up @@ -73,6 +74,9 @@ export default class CartridgeFactory {
case CartridgeInfo.CartridgeType.vanilla_4k:
return new Cartridge4k(buffer);

case CartridgeInfo.CartridgeType.bankswitch_2k_cv:
return new CartridgeCV(buffer);

case CartridgeInfo.CartridgeType.bankswitch_8k_F8:
return new CartridgeF8(buffer);

Expand Down
6 changes: 6 additions & 0 deletions src/machine/stella/cartridge/CartridgeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace CartridgeInfo {
vanilla_2k = 'vanilla_2k',
vanilla_4k = 'vanilla_4k',

bankswitch_2k_cv = 'bankswitch_2k_cv',

bankswitch_8k_F8 = 'bankswitch_8k_F8',
bankswitch_8k_E0 = 'bankswitch_8k_E0',
bankswitch_8k_3F = 'bankswitch_8k_3F',
Expand Down Expand Up @@ -56,6 +58,7 @@ namespace CartridgeInfo {
return [
CartridgeType.vanilla_2k,
CartridgeType.vanilla_4k,
CartridgeType.bankswitch_2k_cv,
CartridgeType.bankswitch_8k_F8,
CartridgeType.bankswitch_8k_E0,
CartridgeType.bankswitch_8k_3F,
Expand Down Expand Up @@ -86,6 +89,9 @@ namespace CartridgeInfo {
case CartridgeType.vanilla_4k:
return 'plain 4k';

case CartridgeType.bankswitch_2k_cv:
return '2k CommaVideo scheme';

case CartridgeType.bankswitch_8k_F8:
return 'bankswitched 8k, F8 (Atari) scheme';

Expand Down

0 comments on commit 26c797f

Please sign in to comment.