Skip to content

Commit 9743550

Browse files
committed
Apple II dynamic origin, fixes #204
1 parent 5eef1b6 commit 9743550

6 files changed

Lines changed: 36 additions & 15 deletions

File tree

src/common/baseplatform.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface Platform {
8585
getPresets?(): Preset[];
8686
pause(): void;
8787
resume(): void;
88-
loadROM(title: string, rom: any); // TODO: Uint8Array
88+
loadROM(title: string, rom: any, origin?: number);
8989
loadBIOS?(title: string, rom: Uint8Array);
9090
getROMExtension?(rom: FileData): string;
9191

@@ -869,8 +869,8 @@ export abstract class BaseMachinePlatform<T extends Machine> extends BaseDebugPl
869869
}
870870
}
871871

872-
loadROM(title, data) {
873-
this.machine.loadROM(data, title);
872+
loadROM(title, data, origin?: number) {
873+
this.machine.loadROM(data, title, origin);
874874
this.reset();
875875
}
876876

src/common/devices.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export interface SampledAudioSource {
6868
}
6969

7070
export interface AcceptsROM {
71-
loadROM(data: Uint8Array, title?: string): void;
71+
loadROM(data: Uint8Array, title?: string, origin?: number): void;
7272
}
7373

7474
export interface AcceptsBIOS {
@@ -247,7 +247,7 @@ export abstract class BasicHeadlessMachine implements HasCPU, Bus, AcceptsROM, P
247247
reset() {
248248
this.cpu.reset();
249249
}
250-
loadROM(data: Uint8Array, title?: string): void {
250+
loadROM(data: Uint8Array, title?: string, origin?: number): void {
251251
if (!this.rom) this.rom = new Uint8Array(this.defaultROMSize);
252252
if (data.length > this.rom.length)
253253
throw new Error(`ROM too big: ${data.length} > ${this.rom.length}}`);

src/common/workertypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export interface WorkerOutputResult<T> {
147147
params?: {}
148148
segments?: Segment[]
149149
debuginfo?: {} // optional info
150+
origin?: number
150151
}
151152

152153
export function isUnchanged(result: WorkerResult) : result is WorkerUnchangedResult {

src/ide/ui.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ async function setCompileOutput(data: WorkerResult) {
926926
try {
927927
clearBreakpoint(); // so we can replace memory (TODO: change toolbar btn)
928928
_resetRecording();
929-
await platform.loadROM(getCurrentPresetTitle(), rom);
929+
await platform.loadROM(getCurrentPresetTitle(), rom, data.origin);
930930
current_output = rom;
931931
if (!userPaused) _resume();
932932
writeOutputROMFile();

src/machine/apple2.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class AppleII extends BasicScanlineMachine implements AcceptsBIOS {
166166
}
167167
this.bios = Uint8Array.from(data);
168168
}
169-
loadROM(data) {
169+
loadROM(data, title?, origin?: number) {
170170
// is it a 16-sector 35-track disk image?
171171
if (data.length == 16 * 35 * 256) {
172172
var diskii = new DiskII(this, data);
@@ -181,17 +181,17 @@ export class AppleII extends BasicScanlineMachine implements AcceptsBIOS {
181181
this.HDR_SIZE = 58;
182182
} else {
183183
// 4-byte DOS header? (TODO: hacky detection)
184-
const origin = this.rom[0] | (this.rom[1] << 8);
184+
const hdrOrigin = this.rom[0] | (this.rom[1] << 8);
185185
const size = this.rom[2] | (this.rom[3] << 8);
186-
let isPlausible = origin < 0xc000
187-
&& origin + size < 0x13000
188-
&& (origin == 0x803 || (origin & 0xff) == 0);
186+
let isPlausible = hdrOrigin < 0xc000
187+
&& hdrOrigin + size < 0x13000
188+
&& (hdrOrigin == 0x803 || (hdrOrigin & 0xff) == 0);
189189
if (size == data.length - 4 && isPlausible) {
190-
this.LOAD_BASE = origin;
190+
this.LOAD_BASE = hdrOrigin;
191191
this.HDR_SIZE = 4;
192192
} else {
193-
// default = raw binary @ $803
194-
this.LOAD_BASE = 0x803;
193+
// Load @ specified origin, fallback to $803
194+
this.LOAD_BASE = origin ?? 0x803;
195195
this.HDR_SIZE = 0;
196196
}
197197
}

src/worker/tools/dasm.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ function parseSymbolMap(asym: string) {
133133
return symbolmap;
134134
}
135135

136+
// Determine likely origin address from listing
137+
function getMinListingOffset(listings: CodeListingMap): number | undefined {
138+
let minOffset: number | undefined;
139+
for (let key in listings) {
140+
let lst = listings[key];
141+
if (lst && lst.lines) {
142+
for (let line of lst.lines) {
143+
if (line.iscode && line.offset > 0) {
144+
if (minOffset === undefined || line.offset < minOffset) {
145+
minOffset = line.offset;
146+
}
147+
}
148+
}
149+
}
150+
}
151+
return minOffset;
152+
}
153+
136154
export function assembleDASM(step: BuildStep): BuildStepResult {
137155
load("dasm");
138156
var unresolved = {};
@@ -216,6 +234,7 @@ export function assembleDASM(step: BuildStep): BuildStepResult {
216234
listings: listings,
217235
errors: errors,
218236
symbolmap: symbolmap,
237+
origin: getMinListingOffset(listings),
219238
};
220239
}
221240

@@ -268,6 +287,7 @@ export function assembleDASM2(step: BuildStep): BuildStepResult {
268287
output,
269288
errors,
270289
listings,
271-
symbolmap
290+
symbolmap,
291+
origin: getMinListingOffset(listings),
272292
};
273293
}

0 commit comments

Comments
 (0)