Skip to content

Commit

Permalink
Refactoring cassette utility
Browse files Browse the repository at this point in the history
  • Loading branch information
ifilot committed Mar 16, 2024
1 parent fca5345 commit 659e10b
Showing 1 changed file with 81 additions and 75 deletions.
156 changes: 81 additions & 75 deletions cassette-utility/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,119 +19,125 @@ void init(void);
int main(void) {
init();

sprintf(&vidmem[0x50*3], "Press any key to read the tape.");
while(keymem[0x0C] == 0) {} // wait until a key is pressed
clearline(3);
// try to access the ROM chip
print_info("Cassette utilty loaded", 0);
print_info("Try to access ROM chip", 1);
uint16_t chip_id = sst39sf_get_chip_id();
sprintf(termbuffer, "Found chip id: %04X", chip_id);
terminal_printtermbuffer();
switch(chip_id) {
case 0xBFB5:
__nrbanks = 2;
print_info("SST39SF010 chip. Capacity: 128kb", 0);
break;
case 0xBFB6:
__nrbanks = 4;
print_info("SST39SF020 chip. Capacity: 256kb", 0);
break;
case 0xBFB7:
__nrbanks = 8;
print_info("SST39SF040 chip. Capacity: 512kb", 0);
break;
default:
print_error("Invalid CHIP id encountered.");
return 0;
break;
}

print_info("Press any key to read the tape.", 1);
wait_for_key();

// rewind the tape
print_info("Rewinding tape...", 1);
tape_rewind();

uint8_t line = 4;
uint8_t iter = 0;
// create placeholders to store tape data
char description[17];
description[16] = '\0';
char ext[4];
ext[3] = '\0';

while(memory[CASSTAT] != 'M') {
// read the first block from the tape; the data from the tape is now
// copied to internal memory
print_info("Reading next program...", 1);
tape_read_block();
if(memory[CASSTAT] != 0) {
sprintf(&vidmem[0x50*20], "Stop reading tape, exit code: %c", memory[CASSTAT]);
sprintf(termbuffer, "%cStop reading tape, exit code: %c", COL_RED, memory[CASSTAT]);
terminal_printtermbuffer();
break;
}

// set file counter
vidmem[0x50*line] = COL_YELLOW;
sprintf(&vidmem[0x50*line+1], "%03i", iter+1);
vidmem[0x50*line+4] = COL_WHITE;

// copy data from description to screen
memcpy(&vidmem[0x50*line+5], &memory[DESC1], 8);
memcpy(&vidmem[0x50*line+5+8], &memory[DESC2], 8);
memcpy(&vidmem[0x50*line+5+17], &memory[EXT], 3);
memcpy(description, &memory[DESC1], 8);
memcpy(&description[8], &memory[DESC2], 8);
memcpy(ext, &memory[EXT], 3);
const uint8_t totalblocks = memory[BLOCKCTR];
uint16_t length = (uint16_t)memory[LENGTH] | ((uint16_t)memory[LENGTH+1] << 8);
vidmem[0x50*line+25] = COL_CYAN;
sprintf(&vidmem[0x50*line+26], "%05i", length);
sprintf(&vidmem[0x50*line+32], "%02i", totalblocks);
vidmem[0x50*line+34] = COL_RED;

// at this point, the data resides in internal memory and the user is
// asked whether they want to store the program from tape on the ROM
// chip or whether they want to continue searching for the next program
// on the tape
sprintf(termbuffer, "Found: %c%s %s%c%i%c%i", COL_YELLOW, description,
ext,COL_CYAN,totalblocks,COL_MAGENTA,length);
terminal_printtermbuffer();
print_info("Copy program to ROM? [Y/N]", 1);

// check if user presses YES key
uint8_t store_continue = wait_for_key_fixed(33);

// grab total blocks and start copying first block
uint8_t blockcounter = 0;
uint16_t bankblock = copyblock(blockcounter, totalblocks, 0xFFFF);
if(store_continue == 1) {
// grab total blocks and start copying first block
uint8_t blockcounter = 0;
uint16_t bankblock = copyblock(blockcounter, totalblocks, 0xFFFF);

if(bankblock == 0xFFFF) { // chip is full, exit
sprintf(&vidmem[0x50*18], "CHIP IS FULL. TERMINATING.");
break;
}

// write start byte
write_startbyte(bankblock);

// consume all blocks
while(memory[BLOCKCTR] > 1) {
blockcounter++;
sprintf(&vidmem[0x50*line+35], "%02i", memory[BLOCKCTR]-1);
tape_read_block();
if(memory[CASSTAT] != 0) {
sprintf(&vidmem[0x50*20], "Stop reading tape, exit code: %c", memory[CASSTAT]);
if(bankblock == 0xFFFF) { // chip is full, exit
print_error("CHIP IS FULL. TERMINATING.");
return 0;
}
bankblock = copyblock(blockcounter, totalblocks, bankblock);
}
vidmem[0x50*line+34] = COL_GREEN;
sprintf(&vidmem[0x50*line+35], "OK");

line++;
iter++;

clearline(3);
sprintf(&vidmem[0x50*3], "Press any key to read the next file.");

keymem[0x0C] = 0;
while(keymem[0x0C] == 0) {} // wait until a key is pressed
clearline(3); // remove line to press any key
// write start byte
write_startbyte(bankblock);

// consume all blocks
while(memory[BLOCKCTR] > 1) {
blockcounter++;
sprintf(termbuffer, "Remaining blocks: %i...", memory[BLOCKCTR]-1);
terminal_redoline();
tape_read_block();
if(memory[CASSTAT] != 0) {
sprintf(termbuffer, "Stop reading tape, exit code: %c", memory[CASSTAT]);
terminal_printtermbuffer();
return 0;
}
bankblock = copyblock(blockcounter, totalblocks, bankblock);
}
sprintf(termbuffer, "%cCopied: %s to ROM", COL_GREEN, description);
terminal_printtermbuffer();
} else {
// skip all blocks
while(memory[BLOCKCTR] > 1) {
sprintf(termbuffer, "Skipping blocks: %i...", memory[BLOCKCTR]-1);
terminal_redoline();
tape_read_block();
}
sprintf(termbuffer, "%cSkipping: %s", COL_RED, description);
terminal_printtermbuffer();
}
}

sprintf(&vidmem[0x50*21], "All done");
print_info("All done", 0);

return 0;
}

void init(void) {
ledbank_init(); // turn all leds off
clear_screen();
terminal_init(3, 21);
terminal_init(3, 20);

sprintf(&vidmem[0x50+2], "%c%cCASSETTE-UTILITY", TEXT_DOUBLE, COL_CYAN);
sprintf(&vidmem[0x50*22], "Version: %s", __VERSION__);
sprintf(&vidmem[0x50*23], "Compiled at: %s / %s", __DATE__, __TIME__);

// determine chip id
uint16_t chip_id = sst39sf_get_chip_id();

switch(chip_id & 0xFF) {
case 0xB5:
sprintf(&vidmem[21], "%s", "SST39SF010 (128kb)");
__nrbanks = 2;
break;
case 0xB6:
sprintf(&vidmem[21], "%s", "SST39SF020 (256kb)");
__nrbanks = 4;
break;
case 0xB7:
sprintf(&vidmem[21], "%s", "SST39SF040 (512kb)");
__nrbanks = 8;
break;
default:
// stop initialization and throw error message
sprintf(&vidmem[0x50 * 10], "%s", "[ERROR]: Cannot detect external chip");
sprintf(&vidmem[0x50 * 11], "%s", "Ensure a ROM is inserted in the data");
sprintf(&vidmem[0x50 * 12], "%s", "cartridge and press RESET");
while(0 == 0){}; // put in infinite loop
}
}

0 comments on commit 659e10b

Please sign in to comment.