Skip to content

Commit

Permalink
making AgonLight serial more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
S0urceror committed Sep 17, 2023
1 parent 5f07a03 commit f32e0cf
Showing 1 changed file with 100 additions and 42 deletions.
142 changes: 100 additions & 42 deletions src/remotes/agonlight/agonelectronhalremote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,50 +233,61 @@ export class AgonElectronHALRemote extends RemoteBase {
});
timeoutid = setTimeout (()=>{
reject (new Error ("serial port blocking read"));
},1000);
},2500);
});
}

public async readLine (): Promise<string> {
var ready:boolean = false;
var line:string = "";
while (!ready) {
var chunk = await this.readChunk ();
var fragment = new TextDecoder("ascii").decode(chunk);
var delimpos = fragment.indexOf ('#');
if (delimpos>=0) {
ready = true;
fragment = fragment.slice (0,delimpos+1);
// if (chunk.length>delimpos+1)
// this.serialPort!.unshift (chunk.slice(delimpos+1));

try {
var ready:boolean = false;
while (!ready) {
var chunk = await this.readChunk ();
var fragment = new TextDecoder("ascii").decode(chunk);
var delimpos = fragment.indexOf ('#');
if (delimpos>=0) {
ready = true;
fragment = fragment.slice (0,delimpos+1);
// if (chunk.length>delimpos+1)
// this.serialPort!.unshift (chunk.slice(delimpos+1));
}
line = line.concat (fragment);
}
line = line.concat (fragment);
console.log ("#"+line.slice (0,line.length-1));
return line;
} catch (e) {
console.log (line);
return "";
}
console.log ("#"+line.slice (0,line.length-1));
return line;
}

public async readBytes (size:number): Promise<Uint8Array> {
var bytes = new Uint8Array(size);
var received=0;
var position=0;

while (true)
{
var chunk = await this.readChunk ();
received = chunk.length;
// prevent buffer overruns
Utility.assert(position+received <= size);
// add to buffer
bytes.set (chunk,position);
position += received;
// done?
if (position == size) {
return (bytes);
}
else {
// console.log ("buffer underrun");
try {
var bytes = new Uint8Array(size);
var received=0;
var position=0;

while (true)
{
var chunk = await this.readChunk ();
received = chunk.length;
// prevent buffer overruns
Utility.assert(position+received <= size);
// add to buffer
bytes.set (chunk,position);
position += received;
// done?
if (position == size) {
return (bytes);
}
else {
// console.log ("buffer underrun");
}
}
} catch (e) {
console.log (e);
return new Uint8Array();;
}
}

Expand All @@ -288,7 +299,7 @@ export class AgonElectronHALRemote extends RemoteBase {
*/
public async disconnect(): Promise<void> {
this.disconnect = async () => {}; // Prohibit that disconnect is executed twice.
if (!this.serialPort) {
if (this.serialPort==undefined) {
return;
}
// remove old breakpoints (if any)
Expand Down Expand Up @@ -317,12 +328,19 @@ export class AgonElectronHALRemote extends RemoteBase {
await sleep (500);
// enter ZDI mode
this.serialPort!.write (new Uint8Array ([0x1a]));
await this.readLine ();
// break
this.serialPort!.write ("b\r\n");
await this.readLine ();
// report success
this.emit ("initialized");
let result = await this.readLine ();
if (result.length==0) {
await this.closeSerialPort();
this.serialPort = undefined;
const err = new Error('ZDI mode not supported');
this.emit('error', err);
} else {
// break
this.serialPort!.write ("b\r\n");
await this.readLine ();
// report success
this.emit ("initialized");
}
}

/**
Expand Down Expand Up @@ -358,7 +376,7 @@ export class AgonElectronHALRemote extends RemoteBase {
Utility.assert(false); // override this
}

/**
/**
* 'continue' debugger program execution.
* @returns A Promise with a string.
* Is called when it's stopped e.g. when a breakpoint is hit.
Expand Down Expand Up @@ -395,6 +413,40 @@ export class AgonElectronHALRemote extends RemoteBase {
}
});
}

// /**
// * 'continue' debugger program execution.
// * @returns A Promise with a string.
// * Is called when it's stopped e.g. when a breakpoint is hit.
// * reason contains the stop reason as string.
// */
// public async continue(): Promise<string> {
// return new Promise<string>(async (resolve) => {
// this.continueResolve = new PromiseCallbacks<string>(this, 'continueResolve', resolve);

// // continue
// this.serialPort!.write ("c\r\n");
// await this.readLine ();
// // wait
// this.serialPort!.write ("w\r\n");

// var ready:boolean = false;
// while (!ready)
// {
// let waitstatus = await this.readLine ();
// if (waitstatus.length>0) {
// this.serialPort!.write ("b\r\n");
// await this.readLine ();
// this.serialPort!.write ("r\r\n");
// let regs = await this.readLine ();
// Z80Registers.setCache(regs);
// await this.getCallStackFromEmulator();
// this.continueResolve!.resolve ('breakpoint: 0x'+Z80Registers.getPC().toString(16));
// ready = true;
// }
// }
// });
// }

/**
* 'pause' the debugger.
Expand Down Expand Up @@ -729,7 +781,13 @@ export class AgonElectronHALRemote extends RemoteBase {
checksum &= 0xff;
line += sprintf ("%02X\r\n",checksum);
this.serialPort!.write (line);
var result = await this.readChunk ();
var result:Uint8Array;
try {
result = await this.readChunk ();
} catch (e) {
console.log (e);
result = new Uint8Array();
}
if (result.length!=1 || result[0]!=46) // 46 = '.' indicates correct checksum, '!' indicates error
throw new Error('Object load error: '+path+" at "+idx);
checksum = 0;
Expand Down

0 comments on commit f32e0cf

Please sign in to comment.