From bc3fbeb48dea39d7213fb257a56e35bcacba9e8d Mon Sep 17 00:00:00 2001 From: Maxwell Beck Date: Sat, 28 Dec 2024 19:09:06 -0600 Subject: [PATCH] Use multicore to meet timing --- CMakeLists.txt | 4 ++-- src/6502.h | 2 +- src/drive.h | 18 +++++++++--------- src/pico1541.c | 37 +++++++++++++++++++++++-------------- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 59c6d70..0f4a923 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ include(pico_sdk_import.cmake) project(pico1541 VERSION 0.1.0) pico_sdk_init() -add_executable(pico1541 src/pico1541.c src/6502.c src/6522.c src/drive.c testbin/1541.o testbin/testimage.o) +add_executable(pico1541 src/pico1541.c src/6502.c src/6522.c src/drive.c testbin/1541.o testbin/bsk1.o) pico_set_binary_type(pico1541 copy_to_ram) -target_link_libraries(pico1541 pico_runtime hardware_timer) +target_link_libraries(pico1541 pico_runtime pico_multicore hardware_timer) pico_add_extra_outputs(pico1541) \ No newline at end of file diff --git a/src/6502.h b/src/6502.h index 1333623..78f03e8 100644 --- a/src/6502.h +++ b/src/6502.h @@ -25,7 +25,7 @@ typedef struct cpu_s { uint8_t x; uint8_t y; uint8_t sp; - uint8_t flags; + volatile uint8_t flags; bool irq; bool nmi; diff --git a/src/drive.h b/src/drive.h index b9fb1f2..c41712a 100644 --- a/src/drive.h +++ b/src/drive.h @@ -11,14 +11,14 @@ extern const uint8_t BITRATE_PER_TRACK[40]; extern const uint8_t GCR_CONV[16]; typedef struct drive_s { - bool spinning; + volatile bool spinning; uint8_t timer; uint8_t *image; - uint8_t track; - uint8_t halftrack; - uint32_t image_ptr; - uint8_t sector; + volatile uint8_t track; + volatile uint8_t halftrack; + volatile uint32_t image_ptr; + volatile uint8_t sector; uint8_t byte; uint16_t gcr_word; @@ -33,10 +33,10 @@ typedef struct drive_s { uint8_t current_idx; uint8_t lookahead_idx; - uint16_t unlatched[DRIVE_LOOKAHEAD]; - uint16_t latched[DRIVE_LOOKAHEAD]; - bool sync[DRIVE_LOOKAHEAD]; - bool byte_ready[DRIVE_LOOKAHEAD]; + volatile uint16_t unlatched[DRIVE_LOOKAHEAD]; + volatile uint16_t latched[DRIVE_LOOKAHEAD]; + volatile bool sync[DRIVE_LOOKAHEAD]; + volatile bool byte_ready[DRIVE_LOOKAHEAD]; } drive_t; void drive_init(drive_t *drive); diff --git a/src/pico1541.c b/src/pico1541.c index f76d8c3..266bfd7 100644 --- a/src/pico1541.c +++ b/src/pico1541.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "6502.h" #include "6522.h" @@ -16,14 +17,14 @@ const uint8_t DEVICE = 8; static uint8_t ram[2048]; extern uint8_t _binary__Users_max_Documents_c64_roms_dos1541_start[16384]; -extern uint8_t _binary__Users_max_Downloads_blackbird_1_2_Blackbird_1_2_d64_start[174848]; -// extern uint8_t _binary__Users_max_Downloads_3sira_still_human_by_arise_3sira_by_arise_A_d64_start[174848]; +// extern uint8_t _binary__Users_max_Downloads_blackbird_1_2_Blackbird_1_2_d64_start[174848]; +extern uint8_t _binary__Users_max_Downloads_Bloodsuckers_StillRising_BSK_StillRising_disk1_d64_start[174848]; -static uint32_t ts; -static cpu_t cpu; +uint32_t ts; +cpu_t cpu; static via_t via1; static via_t via2; -static drive_t drive; +drive_t drive; int8_t track_step; @@ -135,8 +136,22 @@ void atn_irq_callback(uint pin, uint32_t flags) { } } +void drive_thread() { + drive_init(&drive); + drive.image = _binary__Users_max_Downloads_Bloodsuckers_StillRising_BSK_StillRising_disk1_d64_start; + multicore_fifo_push_blocking(1); + + while (true) { + uint32_t cycles = multicore_fifo_pop_blocking_inline(); + if (drive_cycle(&drive, cycles, true) && (via2.pcr & 0b1110) == 0b1110) { + cpu.flags |= 1 << FLAG_V_BIT; + } + } +} + int main() { set_sys_clock_khz(250000, true); + multicore_launch_core1(drive_thread); gpio_init(GPIO_CLK); gpio_init(GPIO_DATA); @@ -163,8 +178,7 @@ int main() { gpio_set_irq_enabled_with_callback(GPIO_ATN, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &atn_irq_callback); track_step = 0; - drive_init(&drive); - drive.image = _binary__Users_max_Downloads_blackbird_1_2_Blackbird_1_2_d64_start; + multicore_fifo_pop_blocking(); cpu_reset(&cpu); via_init(&via1); @@ -173,25 +187,20 @@ int main() { ts = time_us_32(); uint16_t osc2 = 0xffff; while (!cpu.jammed) { - if (ts + 100 < time_us_32()) { + if (ts < time_us_32()) { break; } uint8_t cycles = cpu_step(&cpu); + multicore_fifo_push_blocking_inline((uint32_t)cycles); via_cycle(&via1, cycles); via_cycle(&via2, cycles); cpu.irq = via_irq(&via1) | via_irq(&via2); - if (drive_cycle(&drive, cycles, true) && (via2.pcr & 0b1110) == 0b1110) { - cpu.flags |= 1 << FLAG_V_BIT; - } - ts += cycles; while (ts > time_us_32()) {} } - gpio_put(GPIO_LED, 1); - return 0; } -- 2.47.2