Skip to content

Commit feafccf

Browse files
committed
Added Uart support
1 parent 2c8d801 commit feafccf

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ target_sources(${PROGRAM} PRIVATE main.c bus.c rom.c)
1111
pico_add_extra_outputs(${PROGRAM})
1212

1313
pico_enable_stdio_usb(${PROGRAM} 1)
14-
pico_enable_stdio_uart(${PROGRAM} 1)
14+
pico_enable_stdio_uart(${PROGRAM} 0)
1515

1616
target_link_libraries(${PROGRAM} PRIVATE
1717
pico-56-tms9918

src/boot-menu/boot-menu.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ void runBootMenu()
306306
vrEmuTms9918WriteString(tms9918, "Loading ");
307307
vrEmuTms9918WriteString(tms9918, fileList[currentIndex].fname);
308308

309+
printf("Loading %s...\n", fileList[currentIndex].fname);
310+
309311
FIL fil;
310312
fr = f_open(&fil, fileList[currentIndex].fname, FA_OPEN_EXISTING | FA_READ);
311313
if (fr == FR_OK || fr == FR_EXIST)

src/bus.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "pico/time.h"
2727

2828
#include <stdlib.h>
29+
#include <stdio.h>
2930

3031
// HBC-56 RAM
3132
static 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;

src/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@
6767

6868
#ifdef _WINDOWS
6969
#define HBC56_HAVE_UART 1
70+
#endif
7071
#define HBC56_UART_PORT 0x20
7172
#define HBC56_UART_PORTNAME "COM7"
7273
#define HBC56_UART_CLOCK_FREQ HBC56_CLOCK_FREQ
7374
#define HBC56_UART_IRQ 3
74-
#endif
7575

7676
/* computed configuration values (shouldn't need to touch these)
7777
-------------------------------------------------------------------------- */

src/devices/interrupts/interrupts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void setOrClearInterrupt(int irq, bool doSet)
2121

2222
void raiseInterrupt(int irq)
2323
{
24-
interruptRegister |= 1 << (irq - 1);
24+
interruptRegister |= (1 << (irq - 1));
2525
}
2626

2727
void releaseInterrupt(int irq)

0 commit comments

Comments
 (0)