Skip to content

Commit e36cac0

Browse files
committed
Writing implemented, but not working yet
1 parent 37baef9 commit e36cac0

File tree

12 files changed

+295
-62
lines changed

12 files changed

+295
-62
lines changed

.vscode/settings.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"editor.defaultFormatter": "esbenp.prettier-vscode",
3-
"[javascript]": {
4-
"editor.defaultFormatter": "esbenp.prettier-vscode"
5-
}
6-
}
2+
"editor.defaultFormatter": "esbenp.prettier-vscode",
3+
"[javascript]": {
4+
"editor.defaultFormatter": "esbenp.prettier-vscode"
5+
}
6+
}

6502.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ export class Cpu6502 extends Base6502 {
11581158
this.acia = new Acia(this, this.soundChip.toneGenerator, this.scheduler, this.touchScreen);
11591159
this.serial = new Serial(this.acia);
11601160
this.ddNoise.spinDown();
1161-
this.fdc = new this.model.Fdc(this, this.scheduler);
1161+
this.fdc = new this.model.Fdc(this, this.ddNoise, this.scheduler);
11621162
this.crtc = this.video.crtc;
11631163
this.ula = this.video.ula;
11641164
this.adconverter = new Adc(this.sysvia, this.scheduler);

disc-drive.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ export class DiscDrive {
105105
this._headPosition = 0;
106106
this._checkTrackNeedsWrite();
107107
}
108-
// Forcibly reschedule. Some of the callbacks may "stopSpinning/startSpinning" and thus
109-
// rescheduled this callback already, but we want to override that.
110-
this._timer.reschedule(nextTicks - thisTicks);
111-
this._spinning = true;
108+
if (this._spinning) this._timer.reschedule(nextTicks - thisTicks);
112109
}
113110

114111
get headPosition() {
@@ -142,7 +139,7 @@ export class DiscDrive {
142139
get spinning() {
143140
// beebjit uses the timer's scheduledness here, but our schedule system deschedules timers
144141
// during callbacks, which makes this briefly "false" and disturbs things.
145-
return this._spinning || this._timer.scheduled();
142+
return this._spinning;
146143
}
147144

148145
startSpinning() {
@@ -177,7 +174,16 @@ export class DiscDrive {
177174
}
178175

179176
writePulses(pulses) {
180-
throw new Error(`Not supported: ${pulses}`);
177+
if (!this.disc) return;
178+
// All drives seen have a write protect failsafe on the drive itself.
179+
if (this.disc.writeProtected) return;
180+
if (this._in32usMode) {
181+
if (pulses & 0xffff0000) throw new Error(`Unable to write 32us pulses for ${pulses}`);
182+
const existingPulses = this.disc.readPulses(this._isSideUpper, this.track, this.headPosition);
183+
if (this._pulsePosition === 0) pulses = (existingPulses & 0x0000ffff) | (pulses << 16);
184+
else pulses = (existingPulses & 0xffff0000) | pulses;
185+
}
186+
this.disc.writePulses(this._isSideUpper, this.track, this.headPosition, pulses);
181187
}
182188

183189
get writeProtect() {

disc.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ export class Disc {
272272
return new Disc(true, true, new DiscConfig());
273273
}
274274
/**
275-
* @param {boolean} isWriteable
276-
* @param {boolean} isMutable
277-
* @param {DiscConfig} config
275+
* @param {boolean} isWriteable
276+
* @param {boolean} isMutable
277+
* @param {DiscConfig} config
278278
*/
279279
constructor(isWriteable, isMutable, config) {
280280
this.config = config;
@@ -302,7 +302,11 @@ export class Disc {
302302
this.load();
303303
}
304304

305-
/// @returns {Track}
305+
get writeProtected() {
306+
return !this.isWriteable;
307+
}
308+
309+
/** @returns {Track} */
306310
getTrack(isSideUpper, trackNum) {
307311
return isSideUpper ? this.upperSide.tracks[trackNum] : this.lowerSide.tracks[trackNum];
308312
}
@@ -334,6 +338,26 @@ export class Disc {
334338
return this.getTrack(isSideUpper, track).pulses2Us[position];
335339
}
336340

341+
/**
342+
* @param {boolean} isSideUpper
343+
* @param {Number} track
344+
* @param {Number} position
345+
* @param {Number} pulses
346+
*/
347+
writePulses(isSideUpper, track, position, pulses) {
348+
const trackObj = this.getTrack(isSideUpper, track);
349+
if (position >= trackObj.length)
350+
throw new Error(`Attempt to write off end of track ${position} > ${track.length}`);
351+
if (this.isDirty) {
352+
if (isSideUpper !== this.dirtySide || track !== this.dirtyTrack)
353+
throw new Error("Switched dirty track or side");
354+
}
355+
this.isDirty = true;
356+
this.dirtySide = isSideUpper;
357+
this.dirtyTrack = track;
358+
trackObj.pulses2Us[position] = pulses;
359+
}
360+
337361
flushWrites() {
338362
if (!this.isDirty) {
339363
if (this.dirtySide !== -1 || this.dirtyTrack !== -1) throw new Error("Bad state in disc dirty tracking");

fdc.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,30 @@ export function emptySsd(fdc) {
3232

3333
export function discFor(fdc, name, stringData, onChange) {
3434
const data = typeof stringData !== "string" ? stringData : utils.stringToUint8Array(stringData);
35-
// const prevData = new Uint8Array(data);
36-
37-
//// HACKY MCHACKFACE
38-
const disc = new Disc(false, false, new DiscConfig());
39-
return loadSsd(disc, data, false);
40-
41-
// function changed() {
42-
// let res = false;
43-
// for (let i = 0; i < data.length; ++i) {
44-
// if (data[i] !== prevData[i]) {
45-
// prevData[i] = data[i];
46-
// res = true;
47-
// }
48-
// }
49-
// return res;
50-
// }
51-
52-
// return new BaseDisc(fdc, name, data, (disc) => {
53-
// if (!changed()) return;
54-
// if (onChange) {
55-
// onChange(disc.data);
56-
// }
57-
// });
35+
const prevData = new Uint8Array(data);
36+
37+
if (fdc.isPulseLevel) {
38+
//// TODO: handle onChange
39+
const disc = new Disc(false, false, new DiscConfig());
40+
return loadSsd(disc, data, false);
41+
}
42+
function changed() {
43+
let res = false;
44+
for (let i = 0; i < data.length; ++i) {
45+
if (data[i] !== prevData[i]) {
46+
prevData[i] = data[i];
47+
res = true;
48+
}
49+
}
50+
return res;
51+
}
52+
53+
return new BaseDisc(fdc, name, data, (disc) => {
54+
if (!changed()) return;
55+
if (onChange) {
56+
onChange(disc.data);
57+
}
58+
});
5859
}
5960

6061
export function localDisc(fdc, name) {

0 commit comments

Comments
 (0)