Skip to content

Commit

Permalink
Implemented readBytes() and writeBytes()()
Browse files Browse the repository at this point in the history
Added methods to read/write non-word-aligned byte memory blocks.
  • Loading branch information
ccattuto authored and thegecko committed Jul 19, 2022
1 parent f936491 commit b00c331
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ The `DAP` (Debug Access Port) layer exposes low-level access to ports, registers
- [x] writeMem32()
- [x] readBlock()
- [x] writeBlock()
- [x] readBytes()
- [x] writeBytes()

### Processor

Expand Down
23 changes: 3 additions & 20 deletions examples/rtt/rtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,16 @@ class RTT {
}).join('')
}

async readBytes (addr, size) {
var sizeToRead = size;
let startOffset = addr % 4;
let endOffset = (addr + size) % 4;

if (startOffset) {
addr -= startOffset;
sizeToRead += startOffset;
}

if (endOffset)
sizeToRead += (4 - endOffset);

var data32 = await this.processor.readBlock(addr, sizeToRead / 4);
return new Uint8Array(data32.buffer).slice(startOffset, startOffset + size);
}

async read (bufId) {
var buf = this.bufUp[bufId];

buf.WrOff = await this.processor.readMem32(buf.bufAddr + 12);

if (buf.WrOff > buf.RdOff) {
var data = await this.readBytes(buf.pBuffer + buf.RdOff, buf.WrOff - buf.RdOff);
var data = await processor.readBytes(buf.pBuffer + buf.RdOff, buf.WrOff - buf.RdOff);
} else if (buf.WrOff < buf.RdOff) {
let data1 = await this.readBytes(buf.pBuffer + buf.RdOff, buf.SizeOfBuffer - buf.RdOff);
let data2 = await this.readBytes(buf.pBuffer, buf.WrOff);
let data1 = await processor.readBytes(buf.pBuffer + buf.RdOff, buf.SizeOfBuffer - buf.RdOff);
let data2 = await processor.readBytes(buf.pBuffer, buf.WrOff);
var data = new Uint8Array(data1.length + data2.length);
data.set(data1, 0);
data.set(data2, data1.length);
Expand Down
77 changes: 77 additions & 0 deletions src/dap/adi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,81 @@ export class ADI implements DAP {
index += chunkSize;
}
}

/**
* Read a block of bytes from a memory access port register
* @param register ID of register to read from
* @param count The count of values to read
* @returns Promise of register data
*/
public async readBytes(register: number, count: number): Promise<Uint8Array> {
// read a word-aligned chunk of 32-bit words containing the requested range, then trim it
let bytesToRead = count;
const startOffset = register & 0x03;
const endOffset = (register + count) & 0x03;

// include left-most 32-bit word
if (startOffset) {
register -= startOffset;
bytesToRead += startOffset;
}

// include right-most 32-bit word
if (endOffset) {
bytesToRead += (4 - endOffset);
}

const result = await this.readBlock(register, bytesToRead / 4);
return new Uint8Array(result.buffer).slice(startOffset, startOffset + count);
}

/**
* Write a block of bytes to a memory access port register
* @param register ID of register to write to
* @param values The values to write
* @returns Promise
*/
public async writeBytes(register: number, values: Uint8Array): Promise<void> {
let bytesToWrite = values.length;
let index = 0;

// initial byte write
if ((bytesToWrite > 0) && (register & 0x01)) {
await this.writeMem8(register, values[index]);
bytesToWrite -= 1;
register += 1;
index += 1;
}

// initial 16-bit word write
if ((bytesToWrite > 1) && (register & 0x02)) {
await this.writeMem16(register, values[index] | (values[index + 1] << 8));
bytesToWrite -= 2;
register += 2;
index += 2;
}

// chunk of word-aligned 32-bit words
if (bytesToWrite >= 4) {
const chunkSize = bytesToWrite - bytesToWrite % 4;
const chunk = new Uint32Array(values.slice(index, index + chunkSize).buffer);
await this.writeBlock(register, chunk);
bytesToWrite -= chunkSize;
register += chunkSize;
index += chunkSize;
}

// trailing 16-bit word write
if (bytesToWrite > 1) {
await this.writeMem16(register, values[index] | (values[index + 1] << 8));
bytesToWrite -= 2;
register += 2;
index += 2;
}

// tailing byte write
if (bytesToWrite > 0) {
await this.writeMem8(register, values[index]);
}
}
}
16 changes: 16 additions & 0 deletions src/dap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ export interface DAP {
* @returns Promise
*/
writeBlock(register: number, values: Uint32Array): Promise<void>;

/**
* Read a block of bytes from a memory access port register
* @param register ID of register to read from
* @param count The count of values to read
* @returns Promise of register data
*/
readBytes(register: number, count: number): Promise<Uint8Array>;

/**
* Write a block of bytes to a memory access port register
* @param register ID of register to write to
* @param values The values to write
* @returns Promise
*/
writeBytes(register: number, values: Uint8Array): Promise<void>;
}

export * from './adi';
Expand Down

0 comments on commit b00c331

Please sign in to comment.