Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/readrom #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion component/disassembler/disassembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,6 @@ function Disassembler() {


this.disassemble = disassembleMemory;
this.loadSymbols = loadSymbols;
this.loadFile = loadSymbols;
this.labelAddress = labelAddress;
}
229 changes: 229 additions & 0 deletions hardemu/compactflash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/**
* SPDX-FileCopyrightText: 2024 Zeal 8-bit Computer <contact@zeal8bit.com>
*
* SPDX-License-Identifier: Apache-2.0
*/

function CompactFlash(Zeal) {
const size = 128*KB; // Not realistic but enough for testing
const io_addr_from = 0x70;
const io_addr_to = 0x77;

/* Internal registers */
var cf_data = new Array(size);
let data_ptr = 0;
let data_sec_cnt = 0;
let sector_buffer_idx = 0;
let sector_write = false;
/**
* STATUS_BUSY_BIT = Bit 7
* STATUS_RDY_BIT = Bit 6
* STATUS_DWF_BIT = Bit 5
* STATUS_DSC_BIT = Bit 4
* STATUS_DRQ_BIT = Bit 3
* STATUS_CORR_BIT = Bit 2
* STATUS_ERR_BIT = Bit 0
*/
let status = 0;
let lba_0 = 0;
let lba_8 = 0;
let lba_16 = 0;
let lba_24 = 0;
let sec_cnt = 0;
let sec_it = 0;
let error = 0;
let feature = 0;
/* The sector buffer has a size of 512 bytes */
let sector_buffer = new Array(512);
/* Registers addresses */
let reg_data = io_addr_from + 0;
let reg_feature = io_addr_from + 1;
let reg_error = io_addr_from + 1; // Same as feature
let reg_sec_cnt = io_addr_from + 2;
let reg_lba_0 = io_addr_from + 3;
let reg_lba_8 = io_addr_from + 4;
let reg_lba_16 = io_addr_from + 5;
let reg_lba_24 = io_addr_from + 6;
let reg_command = io_addr_from + 7;
let reg_status = io_addr_from + 7; // Same as command

for (var i = 0; i < cf_data.length; i++) {
cf_data[i] = i;
}

function is_valid_address(read, address) {
return false;
}

function is_valid_port(read, port) {
return (port & 0xff) >= io_addr_from && (port & 0xff) <= io_addr_to;
}

function mem_read(address) {
console.assert("CompactFlash cannot be read from the memory bus");
return 0;
}

function mem_write(address, value) {
}

function io_read(port) {
port &= 0xff;
switch (port) {
case reg_status: return status;
case reg_error: return error;
case reg_sec_cnt: return sec_cnt;
case reg_lba_0: return lba_0;
case reg_lba_8: return lba_8;
case reg_lba_16: return lba_16;
case reg_lba_24: return lba_24;
case reg_data: return process_data();
default: return 0;
}
}

function io_write(port, value) {
port &= 0xff;
value &= 0xff;
switch (port) {
case reg_data:
process_data(value);
break;
case reg_command:
process_command(value);
break;
case reg_feature:
feature = value;
break;
case reg_sec_cnt:
sec_cnt = value;
break;
case reg_lba_0:
lba_0 = value;
break;
case reg_lba_8:
lba_8 = value;
break;
case reg_lba_16:
lba_16 = value;
break;
case reg_lba_24:
/* Only the lowest 4 bits are part of the address */
lba_24 = value & 0xf;
break;
default:
console.log("Unsupported CompactFlash write register " + port.toString());
break;
}
}

function process_data(value) {
if (sector_write) {
/* If the sector count is 0, do nothing... */
if (sec_cnt == 0) {
return;
}

/* Write the byte to the buffer */
sector_buffer[sector_buffer_idx++] = value;
if (sector_buffer_idx == 512) {
/* Flush to the Compact Flash */
for (var i = 0; i < 512; i++)
cf_data[data_ptr * 512 + i] = sector_buffer[i];
sec_cnt--;
}
} else {
/* Read the next byte from the sector buffer */
const data = sector_buffer[sector_buffer_idx];
console.log("CF: reading " + sector_buffer_idx + " = 0x" + data.toString(16));
sector_buffer_idx = (sector_buffer_idx + 1) % 512;
console.assert(sec_cnt != 0);
if (sector_buffer_idx == 0) {
sec_cnt--;
sec_it++;
for (var i = 0; i < 512; i++) {
sector_buffer[i] = cf_data[(data_ptr + sec_it) * 512 + i];
}
}
return data;
}
}

const CMD_NOP = 0x00;

const CMD_READ_SECTOR1 = 0x20;
const CMD_READ_SECTOR2 = 0x21;

const CMD_WRITE_SECTOR1 = 0x30;
const CMD_WRITE_SECTOR2 = 0x31;

const CMD_READ_BUFFER = 0xE4;
const CMD_WRITE_BUFFER = 0xE8;
const CMD_SET_FEATURE = 0xEF;


function process_command(cmd) {
switch (cmd) {
case CMD_NOP:
break;
case CMD_SET_FEATURE:
break;

case CMD_READ_SECTOR1:
case CMD_READ_SECTOR2:
data_ptr = (lba_24 << 24) | (lba_16 << 16) |
(lba_8 << 8) | (lba_0 << 0);
data_sec_cnt = sec_cnt;
sec_it = 0;
// Fall-through
case CMD_READ_BUFFER:
if ((data_ptr + 1) * 512 >= size) {
console.error("CompactFlash: invalid read address");
return;
}
/* Read the data into the sector buffer */
for (var i = 0; i < 512; i++)
sector_buffer[i] = cf_data[data_ptr * 512 + i];
sector_write = false;
/* Sector index becomes 0 */
sector_buffer_idx = 0;
break;


case CMD_WRITE_SECTOR1:
case CMD_WRITE_SECTOR2:
data_ptr = (lba_24 << 24) | (lba_16 << 16) |
(lba_8 << 8) | (lba_0 << 0);
data_sec_cnt = sec_cnt;
sec_it = 0;
// Fall-through
case CMD_WRITE_BUFFER:
sector_write = true;
/* Sector index becomes 0 */
sector_buffer_idx = 0;
break;
}
}


function loadFile(binary) {
if (typeof binary === "string") {
for (var i = 0; i < binary.length; i++)
cf_data[i] = binary.charCodeAt(i);
} else {
for (var i = 0; i < binary.length; i++)
cf_data[i] = binary[i];
}
/* Set the CompactFlash to ready */
status = 1 << 6;
}


this.is_valid_address = is_valid_address;
this.is_valid_port = is_valid_port;
this.mem_read = mem_read;
this.mem_write = mem_write;
this.io_read = io_read;
this.io_write = io_write;
this.loadFile = loadFile;
}
5 changes: 4 additions & 1 deletion hardemu/machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ function Zeal8bitComputer() {
const ds1307 = new I2C_DS1307(this, i2c);
/* We could pass an initial content to the EEPROM, but set it to null for the moment */
const eeprom = new I2C_EEPROM(this, i2c, null);
const devices = [ rom, ram, vchip, pio, keyboard, mmu ];
/* Extensions */
const compactflash = new CompactFlash(this);
const devices = [ rom, ram, vchip, pio, keyboard, mmu, compactflash ];
const zpu = new Z80({ mem_read, mem_write, io_read, io_write });

this.mmu = mmu;
Expand All @@ -157,6 +159,7 @@ function Zeal8bitComputer() {
this.keyboard = keyboard;
this.ds1307 = ds1307;
this.eeprom = eeprom;
this.compactflash = compactflash;
this.devices = devices;
this.zpu = zpu;
this.getCPUState = zpu.getState
Expand Down
2 changes: 1 addition & 1 deletion hardemu/ram.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function RAM() {
console.assert (false, "IO write invalid for SRAM");
}

function loadFile(offset, binary) {
function loadFile(binary, offset) {
for (var i = 0; i < binary.length; i++) {
ram[offset + i ] = binary.charCodeAt(i);
}
Expand Down
28 changes: 13 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,28 +207,25 @@ <h3>CPU registers</h3>
</section>

<section id="romfile">
<div style="display: none;">
<input type="radio" id="os" name="typebin" value="OS" checked>
<label for="os">OS</label><br>
<input type="radio" id="program" name="typebin" value="Program">
<label for="program">Program</label><br>
<input type="text" id="address" name="address" value="0x2000" />
</div>
<label for="file-input">System image:</label>
<label for="file-rom">System image:</label>
<div class="help" title="File to load into the emulated ROM. It take be a single system binary or an image containing the system and a romdisk">?</div>
<input type="file" id="file-input" placeholder="OS file" />
<label for="file-dump">System map file:</label>
<input type="file" id="file-rom" placeholder="OS file" />
<label for="file-map">System map file:</label>
<div class="help" title="Map file (text) generated by z88dk-z80asm containing the symbols of the binary image/file">?</div>
<input type="file" id="file-dump" placeholder="OS dump" />
<label for="eeprom-bin">EEPROM image:</label>
<input type="file" id="file-map" placeholder="OS dump" />
<label for="file-eeprom">EEPROM image:</label>
<div class="help" title="Binary file to load into the emulated 64KB I2C EEPROM">?</div>
<input type="file" id="eeprom-bin" placeholder="EEPROM image" />
<input type="file" id="file-eeprom" placeholder="EEPROM image" />
<label for="file-cf">Compact Flash image:</label>
<div class="help" title="Binary file to load into the emulated Compact Flash">?</div>
<input type="file" id="file-cf" placeholder="Compact Flash image" />
<button id="read-button">Read file(s)</button>
<div id="statuspan">
<h4>Ready?</h4>
<div id="binready" class="status">System image</div>
<div id="romready" class="status">System image</div>
<div id="symready" class="status">Symbols</div>
<div id="eepromready" class="status">EEPROM image</div>
<div id="cfready" class="status">Compact Flash image</div>
</div>
<pre id="file-contents"></pre>
</section>
Expand Down Expand Up @@ -323,7 +320,7 @@ <h4>Ready?</h4>
<ul>
<li><a href="#" id="about" data-target="about-message">About</a></li>
<li><a href="#" id="help" data-target="help-message">Help</a></li>
<li>©2022-2023 Zeal8bit</li>
<li>©2022-2024 Zeal8bit</li>
</ul>
</footer>

Expand All @@ -336,6 +333,7 @@ <h4>Ready?</h4>
<script src="hardemu/i2c.js"></script>
<script src="hardemu/ram.js"></script>
<script src="hardemu/mmu.js"></script>
<script src="hardemu/compactflash.js"></script>
<script src="hardemu/Z80.js"></script>
<script src="view/popup.js"></script>
<script src="hardemu/screen.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dist-win64": "electron-builder --win --x64",
"dist-mac": "electron-builder --mac",
"dist-linux": "electron-builder --linux",
"dbg": "live-server ./ --port=1145"
"dev": "live-server ./ --port=1145"
},
"keywords": [],
"author": {
Expand Down
Loading