controller/Src/control.c

650 lines
15 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <sys/types.h>
#include "control.h"
#include "io.h"
#include "bsp_driver_sd.h"
#include "profiling.h"
// The top 4 address bits determine which device is used. (16 pages, 256 devices)
// 0xFF means that the device does not exist
// WATCH OUT THE DEVICE ADDRESS NEED TO BE REVERSED (MSB IS ON THE RIGHT)
uint8_t memory_map_0[16] = {0b00010000, 0b10001000, 0b01001000, 0b11001000, 0b00101000, 0b10101000, 0b01101000, 0b11101000, 0b00011000, 0b10011000, 0b01011000, 0b11011000, 0b00111000, 0b10111000, 0b01111000, 0b11111000};
uint8_t memory_map_1[16] = {0b00001000, 0b10001000, 0b01001000, 0b11001000, 0b00101000, 0b10101000, 0b01101000, 0b11101000, 0b00011000, 0b10011000, 0b01011000, 0b11011000, 0b00111000, 0b10111000, 0b01111000, 0b11111000};
Control control;
extern UART_HandleTypeDef huart2;
extern uint8_t ack[1];
extern uint8_t nack[1];
#define CPM_RECORD_SIZE 128
#define CPM_RECODS_PER_BLOCK 128
#define CPM_BLOCK_SIZE CPM_RECORD_SIZE*CPM_RECODS_PER_BLOCK
uint8_t get_device(uint16_t address) {
uint8_t page = address >> 12;
if (control.memory_config == 0) {
return memory_map_0[page];
} else if (control.memory_config == 1) {
return memory_map_1[page];
}
return 0xFF;
}
void control_program_eeprom(uint8_t* data, uint16_t length) {
// Take control of the bus
send_busrq(1);
while (!has_busak()) {
control_cycle();
}
enable_address_out(1);
select_device(memory_map_0[0]);
for (uint16_t i = 0; i < length; ++i) {
write_address(i);
write_data(data[i]);
enable_data_out(1);
send_memrq(1);
send_wr(1);
send_wr(0);
enable_data_out(0);
send_rd(1);
for (;;) {
uint8_t d = read_data();
if (d == data[i]) {
break;
}
}
send_rd(0);
send_memrq(0);
uint8_t progress[] = {(i+1) & 0xFF, (i+1) >> 8};
HAL_UART_Transmit(&huart2, progress, sizeof(progress), HAL_MAX_DELAY);
}
enable_address_out(0);
// Release the bus again
send_busrq(0);
// Restart the z80
control_reset();
HAL_UART_Transmit(&huart2, ack, sizeof(ack), HAL_MAX_DELAY);
}
void handle_memrq() {
uint16_t address = read_address();
uint8_t device = get_device(address);
select_device(device);
}
void handle_io_read() {
uint8_t address = read_address() & 0xFF;
switch (address) {
// @todo This should be detected on startup
// Stand in for graphics hardware
/* case 0x03: */
/* write_data(0x01); */
/* break; */
/* Stand in for the keyboard hardware */
/* case 0x1E: */
/* write_data(char_c); */
/* char_r = 0; */
/* break; */
/* Stand in for the keyboard hardware */
/* case 0x1F: */
/* #<{(| write_data(0x01 * char_r); |)}># */
/* write_data(0x00); */
/* break; */
// Read byte from disk
case 0x08:
if (control.storage.ready && control.storage.command == 0x20) {
write_data(control.storage.buffer[control.storage.counter]);
control.storage.counter++;
if (control.storage.counter >= control.storage.size) {
control.storage.command = 0;
control.storage.ready = 0;
}
} else {
write_data(0x00);
}
break;
// Check if disk is ready
case 0x0f:
if (control.storage.command == 0x30 && !control.storage.ready) {
printf("Write: 0x%lx\n\r", control.storage.lba * CPM_RECORD_SIZE);
for (int i = 0; i < 128; ++i) {
control.storage.buffer[i] = 0x00;
}
control.storage.ready = 1;
}
if (control.storage.command == 0x20 && !control.storage.ready) {
printf("Read: 0x%lx\n\r", control.storage.lba * CPM_RECORD_SIZE);
FRESULT fr = f_lseek(control.storage.file, control.storage.lba * CPM_RECORD_SIZE);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
fr = f_read(control.storage.file, control.storage.buffer, CPM_RECORD_SIZE, &control.storage.size);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
control.storage.ready = 1;
}
write_data(0x08*control.storage.ready);
break;
default: {
/* uint8_t value = read_data(); */
/* #<{(| if (value == 0) { |)}># */
/* printf("IO Read: %.2X @ %.2X\n\r", value, address); */
/* #<{(| } |)}># */
return;
}
}
enable_data_out(1);
}
void handle_io_write() {
uint8_t address = read_address() & 0xFF;
uint8_t value = read_data();
switch (address) {
case 0x00:
control.memory_config = 0;
break;
case 0x01:
control.memory_config = 1;
break;
case 0x02:
printf("%c", value);
break;
// Write byte to disk
case 0x08:
if (control.storage.ready && control.storage.command == 0x30) {
control.storage.buffer[control.storage.counter] = value;
control.storage.counter++;
// Commit changes
if (control.storage.counter == CPM_RECORD_SIZE) {
FRESULT fr = f_lseek(control.storage.file, control.storage.lba * CPM_RECORD_SIZE);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
unsigned int bytes_written;
fr = f_write(control.storage.file, control.storage.buffer, CPM_RECORD_SIZE, &bytes_written);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
if (control.storage.lba >= 0x100 && control.storage.lba < 0x180) {
for (int i = 0; i < 4; ++i) {
DiskEntry entry;
entry.user = control.storage.buffer[32*i + 0];
printf("User: %i\n\r", entry.user);
for (int j = 0; j < 8; ++j) {
char c = control.storage.buffer[32*i + 1 + j];
if (c == ' ') {
c = 0x00;
}
entry.name[j] = c;
}
printf("Name: %.8s\n\r", entry.name);
for (int j = 0; j < 3; ++j) {
char c = control.storage.buffer[32*i + 9 + j];
if (c == ' ') {
c = 0x00;
}
entry.ext[j] = c;
}
printf("Ext: %.3s\n\r", entry.ext);
entry.size = control.storage.buffer[32*i + 15];
for (int j = 0; j < 8; ++j) {
uint16_t allocation = control.storage.buffer[32*i + 2*j + 16];
allocation += control.storage.buffer[32*i + 2*j + 17] << 8;
entry.allocation[j] = allocation;
printf("Allocation: %i\n\r", allocation);
if (j && allocation) {
entry.size += 0x80;
}
}
printf("Size: %li records (%li)\n\r", entry.size, entry.size * CPM_RECORD_SIZE);
char filename[128];
snprintf(filename, 128, "0:A/%.8s.%.3s", entry.name, entry.ext);
FIL file;
if (entry.user == 0xe5) {
FILINFO nfo;
fr = f_stat(filename, &nfo);
if (fr == FR_OK) {
printf("Removing file %s\n\r", filename);
fr = f_unlink(filename);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
}
} else {
printf("Opening %s\n\r", filename);
fr = f_open(&file, filename, FA_WRITE | FA_CREATE_ALWAYS);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
for (int j = 0; j < 8; ++j) {
if (entry.allocation[j] == 0) {
continue;
}
uint8_t buffer[CPM_BLOCK_SIZE] = {0};
printf("Copying allocation %i (%i) to %s\n\r", j, entry.allocation[j], filename);
fr = f_lseek(control.storage.file, 0x8000 + entry.allocation[j]*CPM_BLOCK_SIZE);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
unsigned int bytes_read;
fr = f_read(control.storage.file, buffer, entry.size * CPM_RECORD_SIZE, &bytes_read);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
printf("Bytes read: %i\n\r", bytes_read);
fr = f_lseek(&file, j*CPM_BLOCK_SIZE);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
unsigned int bytes_written;
fr = f_write(&file, buffer, bytes_read, &bytes_written);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
printf("Bytes written: %i\n\r", bytes_written);
f_sync(control.storage.file);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
}
fr = f_close(&file);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
}
}
// @todo Verify that this is how it works
// If there was a write here a file got modified
printf("File(s) in record %li (0x%lx) changed\n\r", control.storage.lba, control.storage.lba * CPM_RECORD_SIZE);
}
control.storage.command = 0;
control.storage.ready = 0;
}
}
break;
case 0x0b: {
uint32_t temp = control.storage.lba & 0xFFFF00;
control.storage.lba = temp + value;
break;
}
case 0x0c: {
uint32_t temp = control.storage.lba & 0xFF00FF;
control.storage.lba = temp + (value << 8);
break;
}
case 0x0d: {
uint32_t temp = control.storage.lba & 0x00FFFF;
control.storage.lba = temp + (value << 16);
break;
}
// Receive disk command
case 0x0f: {
control.storage.ready = 0;
control.storage.counter = 0;
control.storage.command = value;
break;
}
default:
printf("IO Write: %.2X @ %.2X\n\r", value, address);
break;
}
}
void handle_ioreq() {
if (has_wr()) {
handle_io_write();
} else if (has_rd()) {
handle_io_read();
}
}
void control_cycle() {
set_clock(1);
// We need this not detect IO multiple times
static uint8_t had_ioreq = 0;
if (!has_ioreq()) {
had_ioreq = 0;
}
// @todo We are forgetting to set this somewhere
enable_data_out(0);
if (has_memrq()) {
handle_memrq();
} else if (has_ioreq() && !has_m1()) {
had_ioreq++;
if (had_ioreq == 3) {
handle_ioreq();
}
} else if (has_ioreq() && has_m1()) {
printf("Interrupt ackknowledged\n\r");
}
set_clock(0);
}
void copy_file_to_image(char* filename, FIL* disk, uint32_t source, uint32_t destination, uint32_t size) {
FIL file;
FRESULT fr = f_open(&file, filename, FA_READ);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
fr = f_lseek(&file, source);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
uint8_t* buffer = malloc(size);
unsigned int bytes_read;
fr = f_read(&file, buffer, size, &bytes_read);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
fr = f_lseek(disk, destination);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
printf("copying: %s %lx @ %lx (%i)\n\r", filename, source, destination, bytes_read);
unsigned int bytes_written;
fr = f_write(disk, buffer, bytes_read, &bytes_written);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
fr = f_close(&file);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
free(buffer);
}
DiskEntry* get_disk_entries(char* diskpath) {
printf("Scanning: %s\n\r", diskpath);
DiskEntry* entries = malloc(127*sizeof(DiskEntry));
// Clear all entries
for (int i = 0; i < 127; ++i) {
entries[i].user = 0xe5;
for (int j = 0; j < 8; ++j) {
entries[i].name[j] = 0x00;
}
for (int j = 0; j < 3; ++j) {
entries[i].ext[j] = 0x00;
}
entries[i].size = 0;
for (int j = 0; j < 8; ++j) {
entries[i].allocation[j] = 0x00;
}
}
DIR dir;
FILINFO fno;
FRESULT fr = f_opendir(&dir, diskpath);
if (fr == FR_OK) {
uint16_t end = 1;
uint8_t index = 0;
for (;;) {
fr = f_readdir(&dir, &fno);
if (fr != FR_OK || fno.fname[0] == 0) {
break;
}
if (!(fno.fattrib & AM_DIR)) {
entries[index].user = 0x00;
// Calculate the number of records
uint32_t size = (fno.fsize + CPM_RECORD_SIZE - 1) / CPM_RECORD_SIZE;
entries[index].allocation[0] = end;
end++;
int i = 1;
while (size >= 0x80) {
size -= 0x80;
entries[index].allocation[i] = end;
end++;
i++;
}
// Store the size in the number of records
entries[index].size = size;
/* free(entries[index].filename); */
entries[index].filename = strdup(fno.fname);
uint8_t name_len = 9;
for (uint8_t i = 0; i < 8; ++i) {
char c = ' ';
if (name_len == 9 && fno.fname[i] != '.') {
c = fno.fname[i];
} else if (name_len == 9){
name_len = i+1;
}
entries[index].name[i] = c;
}
uint8_t done = 0;
for (uint8_t i = 0; i < 3; ++i) {
char c = ' ';
if (!done && fno.fname[name_len+i] != 0) {
c = fno.fname[name_len+i];
} else {
done = 1;
}
entries[index].ext[i] = c;
}
index++;
}
}
} else {
printf("File error: %i\n\r", fr);
return entries;
}
fr = f_closedir(&dir);
if (fr) {
printf("File error: %i\n\r", fr);
return entries;
}
return entries;
}
// @todo Properly reset everything
void control_reset() {
free(control.storage.buffer);
f_close(control.storage.file);
free(control.storage.file);
f_mount(0, "0:", 0);
free(control.storage.fs);
Control temp = {0, {NULL, NULL, 0, 0, 0, 0, NULL, 0}};
control = temp;
control.storage.buffer = (uint8_t*)malloc(CPM_RECORD_SIZE);
control.storage.fs = (FATFS*)malloc(sizeof(FATFS));
control.storage.file = (FIL*)malloc(sizeof(FIL));
FRESULT fr = f_mount(control.storage.fs, "0:", 0);
if (fr) {
printf("File error: %i\n\r", fr);
}
// Open the image file
fr = f_open(control.storage.file, "0:A.img", FA_READ | FA_WRITE | FA_CREATE_ALWAYS);
if (fr) {
printf("File error: %i\n\r", fr);
}
fr = f_chmod("0:A.img", AM_HID, 0xFF);
if (fr) {
printf("File error: %i\n\r", fr);
}
copy_file_to_image("0:loader.bin", control.storage.file, 0x00, 0x00, 0x80);
copy_file_to_image("0:cpm22.bin", control.storage.file, 0x00, 0x80, 0x1600);
copy_file_to_image("0:bios.bin", control.storage.file, 0x00, 0x1680, 0x600);
f_lseek(control.storage.file, 0x8000);
DiskEntry* entries = get_disk_entries("0:A");
for (int i = 0; i < 127; ++i) {
char buffer[32];
buffer[0] = entries[i].user;
for (int j = 0; j < 8; ++j) {
buffer[1+j] = entries[i].name[j];
}
for (int j = 0; j < 3; ++j) {
buffer[9+j] = entries[i].ext[j];
}
uint16_t extents = 0x00;
for (int j = 1; j < 8; ++j) {
if (entries[i].allocation[j]) {
extents++;
}
}
buffer[12] = extents & 0xFF;
buffer[13] = 0x00;
buffer[14] = (extents >> 8) & 0xFF;
buffer[15] = entries[i].size;
for (int j = 0; j < 8; ++j) {
uint16_t allocation = entries[i].allocation[j];
buffer[16 + j*2] = allocation & 0xFF;
buffer[17 + j*2] = (allocation >> 8) & 0xFF;
}
unsigned int bytes_written;
fr = f_write(control.storage.file, buffer, 32, &bytes_written);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
}
for (int i = 0; i < 127; ++i) {
if (entries[i].user == 0xe5) {
continue;
}
char buf[128];
snprintf(buf, 128, "0:A/%s", entries[i].filename);
for (int j = 0; j < 8; ++j) {
uint16_t allocation = entries[i].allocation[j];
if (allocation) {
printf("allocation: %s (%i)\n\r", buf, allocation);
copy_file_to_image(buf, control.storage.file, j*CPM_BLOCK_SIZE, 0x8000+allocation*CPM_BLOCK_SIZE, CPM_BLOCK_SIZE);
}
}
}
f_sync(control.storage.file);
if (fr) {
printf("File error: %i\n\r", fr);
return;
}
// @todo Pretty sure we have to free things within DiskEntry
free(entries);
set_reset(1);
for (int i = 0; i <= 10; ++i) {
set_clock(i % 2);
}
set_reset(0);
}