2626#include "pico/time.h"
2727
2828#include <stdlib.h>
29+ #include <stdio.h>
2930
3031 // HBC-56 RAM
3132static uint8_t __aligned (4 ) ram [HBC56_RAM_SIZE ];
@@ -60,6 +61,14 @@ FIL fil;
6061
6162#define CPU_6502_WAI 0xcb
6263
64+ #define UART_STATUS_RX_REG_FULL 0b00000001
65+ #define UART_STATUS_TX_REG_EMPTY 0b00000010
66+
67+ static uint8_t uartControl = 0 ;
68+ static uint8_t uartStatus = UART_STATUS_TX_REG_EMPTY ;
69+ static uint8_t uartBuffer = 0 ;
70+
71+
6372/*
6473 * 65c02 bus read/write callbacks
6574 */
@@ -160,6 +169,7 @@ void __not_in_flash_func(busMainLoop)()
160169
161170 absolute_time_t startTime = get_absolute_time ();
162171 absolute_time_t currentTime = startTime ;
172+ absolute_time_t nextUartTime = startTime ;
163173
164174 int i = 0 ;
165175 int prevViaInt = IntCleared ;
@@ -183,6 +193,26 @@ void __not_in_flash_func(busMainLoop)()
183193 // run the via for a number of ticks
184194 vrEmu6522Ticks (via , TICKS_PER_BURST );
185195
196+ if (uartBuffer == 0 )
197+ {
198+ nextUartTime = delayed_by_us (currentTime , 100 );
199+ int c = getchar_timeout_us (0 );
200+ if (c != PICO_ERROR_TIMEOUT )
201+ {
202+ // putchar(c);
203+ uartBuffer = c ;// & 0xff;
204+ raiseInterrupt (HBC56_UART_IRQ );
205+ uartStatus = UART_STATUS_TX_REG_EMPTY ;
206+ uartStatus |= UART_STATUS_RX_REG_FULL ;
207+ }
208+ else
209+ {
210+ releaseInterrupt (HBC56_UART_IRQ );
211+ uartStatus &= ~(UART_STATUS_RX_REG_FULL );
212+ }
213+ }
214+
215+
186216 // has the via interrupted?
187217 setOrClearInterrupt (HBC56_VIA_IRQ , * vrEmu6522Int (via ) == IntRequested );
188218
@@ -202,6 +232,7 @@ void __not_in_flash_func(busMainLoop)()
202232 }
203233}
204234
235+
205236/*
206237 * 65c02 write to the bus
207238 */
@@ -243,6 +274,22 @@ void __not_in_flash_func(busWrite)(uint16_t addr, uint8_t val)
243274 audioWritePsg1 (addr , val );
244275 break ;
245276
277+ case HBC56_UART_PORT :
278+ uartControl = val ;
279+ if ((val & 0x03 ) == 0x03 ) // reset
280+ {
281+ uartStatus = UART_STATUS_TX_REG_EMPTY ;
282+ }
283+ else
284+ {
285+ releaseInterrupt (HBC56_UART_IRQ );
286+ }
287+ break ;
288+
289+ case HBC56_UART_PORT | 0x01 :
290+ putchar (val );
291+ break ;
292+
246293 case FOPEN_PORT :
247294 {
248295 uint16_t addr = ram [val ] | (ram [val + 1 ] << 8 );
@@ -341,6 +388,23 @@ uint8_t __not_in_flash_func(busRead)(uint16_t addr, bool isDbg)
341388 case HBC56_AY38910_B_PORT | 0x02 :
342389 return audioReadPsg1 ();
343390
391+ case HBC56_UART_PORT :
392+ return uartStatus ;
393+
394+ case HBC56_UART_PORT | 0x01 :
395+ {
396+ int c = getchar_timeout_us (0 );
397+ if (c == PICO_ERROR_TIMEOUT )
398+ {
399+ releaseInterrupt (HBC56_UART_IRQ );
400+ uartStatus &= ~(UART_STATUS_RX_REG_FULL );
401+ c = 0 ;
402+ }
403+ uint8_t val = uartBuffer ;
404+ uartBuffer = c ;
405+ return val ;
406+ }
407+
344408 case FCLOSE_PORT :
345409 f_close (& fil );
346410 return 0 ;
0 commit comments