From 86f5af5ca9214d3681058c5252747097fbd6f3d4 Mon Sep 17 00:00:00 2001 From: Mateusz Holenko Date: Wed, 24 Jul 2019 14:55:47 +0200 Subject: [PATCH] Fix handling LiteX uart and timer. --- src/main/c/emulator/src/hal.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/c/emulator/src/hal.c b/src/main/c/emulator/src/hal.c index a9189f2..5a151bb 100644 --- a/src/main/c/emulator/src/hal.c +++ b/src/main/c/emulator/src/hal.c @@ -190,28 +190,44 @@ void stopSim(){ } void putC(char ch){ + // protect against writing to a full tx fifo + while(uart_txfull_read()); uart_rxtx_write(ch); } int32_t getC(){ - return uart_rxempty_read() - ? -1 - : uart_rxtx_read(); + if(uart_rxempty_read()) + { + return -1; + } + + // this is required to refresh rexempty status + uart_ev_pending_write(1 << 1); + return uart_rxtx_read(); } uint32_t rdtime(){ - return (uint32_t)cpu_timer_time_read(); + cpu_timer_latch_write(0); + uint32_t result = (uint32_t)cpu_timer_time_read(); + cpu_timer_latch_write(1); + return result; } uint32_t rdtimeh(){ - return (uint32_t)(cpu_timer_time_read() >> 32); + cpu_timer_latch_write(0); + uint32_t result = (uint32_t)(cpu_timer_time_read() >> 32); + cpu_timer_latch_write(1); + return result; } void setMachineTimerCmp(uint32_t low, uint32_t high){ + cpu_timer_latch_write(0); cpu_timer_time_cmp_write((((unsigned long long int)high) << 32) | low); + cpu_timer_latch_write(1); } void halInit(){ + cpu_timer_latch_write(1); } #endif