Merge branch '14-terminal-driver' into 'master'

Resolve "Terminal Driver"

Closes #14

See merge request bslathi19/super6502!58
This commit is contained in:
Byron Lathi
2023-12-01 09:13:39 +00:00
13 changed files with 471 additions and 14 deletions

View File

@@ -11,7 +11,7 @@ TEST_PROGRAM?=$(REPO_TOP)/sw/test_code/$(TEST_PROGRAM_NAME)/$(TEST_PROGRAM_NAME)
STANDALONE_TB= interrupt_controller_tb mapper_tb rtc_tb uart_irq_tb
CODE_TB= interrupt_controller_code_tb mapper_code_tb rtc_code_tb \
devices_setup_code_tb
devices_setup_code_tb uart_irq_code_tb
#TODO implement something like sources.list

View File

@@ -0,0 +1,36 @@
`timescale 1ns/1ps
module uart_irq_code_tb();
sim_top u_sim_top();
always begin
if (
u_sim_top.w_cpu_addr == 16'h0 &&
u_sim_top.w_cpu_we == '1
) begin
if (u_sim_top.w_cpu_data_from_cpu == 8'h6d) begin
$display("Good finish!");
$finish();
end else begin
$display("Bad finish!");
$finish_and_return(-1);
end
end
# 1;
end
initial begin
repeat(15000) @(posedge u_sim_top.r_clk_cpu)
u_sim_top.u_sim_uart.tx_en = 1;
@(posedge u_sim_top.r_clk_cpu);
u_sim_top.u_sim_uart.tx_data = 8'hAA;
repeat (100) @(posedge u_sim_top.r_clk_cpu);
$finish();
end
always @(u_sim_top.u_dut.u_rtc.r_output) begin
$display("counter: %d", u_sim_top.u_dut.u_rtc.r_output);
end
endmodule

View File

@@ -339,12 +339,16 @@ token = 0xFF - response timeout
*******************************************************************************/
uint8_t SD_writeSingleBlock(uint32_t addr, uint8_t *buf, uint8_t *token)
{
(void)addr;
(void)buf;
(void)token;
/*
uint16_t readAttempts;
uint8_t res1, read;
uint16_t i;
/*
// set token to none
*token = 0xFF;
@@ -391,9 +395,11 @@ uint8_t SD_writeSingleBlock(uint32_t addr, uint8_t *buf, uint8_t *token)
spi_deselect(0);
spi_exchange(0xFF);
return res1;
*/
return res1;
return 0;
}
/*******************************************************************************

View File

@@ -0,0 +1,17 @@
#ifndef _SERIAL_H
#define _SERIAL_H
#include <stdint.h>
void serial_handle_irq();
void serial_init();
void serial_putc(char c);
void serial_puts(char* s);
char serial_getc();
char serial_getc_nb();
#endif

View File

@@ -0,0 +1,94 @@
.MACPACK generic
.autoimport
.import _enable_irq, _send_eoi, _uart_txb_block
.importzp tmp1, ptr1
.export _serial_init
.export _serial_handle_irq
.export _serial_putc, _serial_puts, _serial_getc, _serial_getc_nb
.zeropage
last_char: .res 1
.code
UART = $efe6
UART_TXB = UART
UART_RXB = UART
UART_STATUS = UART + 1
; Initialize Serial Port
; No initialization needed, just register irq handler.
.proc _serial_init
lda #<_serial_handle_irq
ldx #>_serial_handle_irq
jsr pushax
lda #$01
jsr _register_irq
stz last_char
rts
.endproc
; Serial Port IRQ Handler
; Get the character and store it.
.proc _serial_handle_irq
lda UART_RXB
ora #$80 ; set msb
sta last_char
jsr _send_eoi
rti
.endproc
; Serial Port Get Character
; If a character has not been received, block until one is.
.proc _serial_getc
L1: lda last_char
bpl L1
and #$7f
sta last_char
rts
.endproc
; Serial Port Get Character Non-Blocking
; return last character, even if it has already been read.
; If the character is new, we still clear the new flag.
.proc _serial_getc_nb
lda last_char
bpl L1
and #$7f
sta last_char
L1: rts
.endproc
; Serial Port Put Character
; send a single character over the serial port.
.proc _serial_putc
jsr _uart_txb_block
cmp #$0a
bne @1
lda #$0d
jsr _uart_txb_block
@1: rts
.endproc
; Send a string over the serial port. Needs stlen
.proc _serial_puts
sta ptr1 ; Store pointer in ptr1
stx ptr1+1
ldy #$00 ; initialize y to 0
L1: lda (ptr1),y ; load character from string
beq L2 ; Quit if NULL
jsr _serial_putc ; send character (does not change y or ptr1)
iny ; increment y
bne L1 ; If Y == 0, increment high byte of pointer
inc ptr1+1
bne L1 ; if high byte wraps around, give up
L2: rts
.endproc

View File

@@ -0,0 +1,13 @@
#ifndef _TERMINAL_H
#define _TERMINAL_H
#include <stdint.h>
int8_t terminal_read(uint8_t fd, void* buf, uint8_t nbytes);
int8_t terminal_write(uint8_t fd, const void* buf, uint8_t nbytes);
int8_t terminal_open(const uint8_t* filename);
int8_t terminal_close(uint8_t fd);
#endif /* _TERMINAL_H */

View File

@@ -0,0 +1,145 @@
.MACPACK generic
.autoimport
.importzp tmp1, ptr1
.import _serial_getc
.export _terminal_read, _terminal_write, _terminal_open, _terminal_close
.data
terminal_buf: .res 128
.code
; int8_t terminal_read(uint8_t fd, void* buf, uint8_t nbytes);
; Read up to size-1 (127 max) before the enter key is pressed.
; A newline character is automatically added.
; Inputs: int8_t* buf - where input characters are stored
; uint8_t n - number of characters to read (max buf size minux 1)
; Return Value: number of characters read on success, -1 on failure
; Function: Reads keyboard input
.proc _terminal_read
cmp #$00 ; Check that nbytes is > 0 and < 128
beq FAIL
cmp #$81
bge FAIL
sta tmp1 ; Store nbytes in tmp1
jsr popax ; Check that buf != NULL
cmp #$00
bne L1
cpx #$00
bne L1
bra FAIL
; while i < nbytes, store getc into terminal_buf y
L1: sta ptr1
stx ptr1+1
ldy #$00
LOOP: cpy tmp1
bge END
jsr _serial_getc
sta terminal_buf,y
cmp #$0d ; If newline, do something
bne L2
lda #$0a ; hacky serial, we want $0a, not $0d
jsr _serial_putc
bra END
L2: cmp #$08 ; Handle backspace
bne L3
lda tmp1
beq LOOP
lda #$08
jsr _serial_putc
dey
lda #$00
sta terminal_buf,y
bra LOOP
L3: lda terminal_buf,y ; Normal character
sta (ptr1),y
jsr _serial_putc
iny
bra LOOP
END: phy ; Zero out terminal buffer
ldy #$00
lda #$00
L4: sta terminal_buf,y
iny
cpy #$80
blt L4
L5: ply ; End string with NULL
lda #$0a
sta (ptr1),y
iny
cpy #$80 ; But not if we are at max
bge L6
lda #$00
sta (ptr1),y
L6: lda #$00 ; Return - on success
rts
FAIL: lda #$ff ; return -1 on fail
rts
.endproc
; int8_t terminal_write(uint8_t fd, const void* buf, uint8_t nbytes);
; write characters to the terminal
; Inputs: int8_t* buf - buffer of characters to write
; uint8_t n - number of characters to write
; Return Value: 0 on success, -1 on failure
; Writes to screen. Only stops after n chars written.
; This seems to not care about null termination?
.proc _terminal_write
sta tmp1 ; put nbytes in tmp1
jsr popax ; check that buf is not null
cmp #$00
bne L1
cpx #$00
bne L1
bra FAIL
L1: sta ptr1
stx ptr1+1
ldy #$00
LOOP: lda (ptr1),y ; Loop through buffer and print
jsr _serial_putc
iny
cpy tmp1
blt LOOP
lda #$00
rts
FAIL: lda #$ff ; old code didn't fail, but new code does.
rts
.endproc
; terminal_open
; open terminal device
; Inputs: uint8_t* filename
; Outputs: none
; Return Value: 0 on success
; Function: none.
.proc _terminal_open
lda #$0
rts
.endproc
; terminal_close
; close terminal device
; Inputs: int32_t fd
; Outputs: none
; Return Value: 0 on success (but this always failes)
; Function: none.
.proc _terminal_close
lda #$ff ; -1
rts
.endproc

View File

@@ -48,7 +48,7 @@ rti
sta IRQ_CMD_ADDR
lda IRQ_DAT_ADDR
; shift by 2 (oh so only 128 interrupts are supported lol)
lsr
asl
tax
jmp (irq_table,x)
; use that to index jump table
@@ -56,6 +56,7 @@ rti
; void register_irq(void* addr, uint8_t irqn);
.proc _register_irq
asl
tax
jsr popa
sta irq_table,x
@@ -65,6 +66,7 @@ rti
lda #$00
jsr pusha
txa
lsr
jsr _enable_irq
rts
.endproc

View File

@@ -1,31 +1,37 @@
#include <conio.h>
#include <string.h>
#include "devices/interrupt_controller.h"
#include "interrupts/interrupt.h"
#include "devices/mapper.h"
#include "devices/rtc.h"
#include "devices/serial.h"
#include "devices/terminal.h"
void handle_rtc_interrupt() {
// cputs("In IRQ interrupt!\n");
cputc('A');
// cputc('A');
send_eoi();
asm volatile ("rti");
}
int main() {
char buf[128];
int main() {
cputs("Kernel\n");
cputs("Init Mapper\n");
// cputs("Init Mapper\n");
init_mapper();
cputs("Initialize Interrupts\n");
// cputs("Initialize Interrupts\n");
init_interrupts();
cputs("Initialize Interrupt Controller\n");
// cputs("Initialize Interrupt Controller\n");
init_interrupt_controller();
cputs("Initialize RTC\n");
// cputs("Initialize RTC\n");
init_rtc();
register_irq(&handle_rtc_interrupt, 0);
@@ -33,10 +39,22 @@ int main() {
asm volatile("cli");
// cputs("Initialize Serial\n");
// // init_serial();
// enable_irq(2, IRQ_EDGE);
serial_init();
while(1);
serial_puts("Hello from serial!\n");
terminal_open(NULL);
terminal_write(0, "Terminal Write\n", 15);
while(1) {
if (terminal_read(0, buf, 128)) {
cprintf("Fail\n");
break;
}
terminal_write(0, "Got: ", 5);
terminal_write(0, buf, strlen(buf));
terminal_write(0, "\n", 1);
}
return 0;
}

View File

@@ -0,0 +1,42 @@
CC=../../cc65/bin/cl65
LD=../../cc65/bin/cl65
CFLAGS=-T -t none -I. --cpu "65C02"
LDFLAGS=-C link.ld -m $(NAME).map
NAME=uart_irq_test
DEVICES=$(REPO_TOP)/sw/kernel/devices
BIN=$(NAME).bin
HEX=$(NAME).hex
LISTS=lists
SRCS=$(wildcard *.s) $(wildcard *.c)
SRCS+=$(DEVICES)/interrupt_controller.s
SRCS+=$(wildcard **/*.s) $(wildcard **/*.c)
OBJS+=$(patsubst %.s,%.o,$(filter %s,$(SRCS)))
OBJS+=$(patsubst %.c,%.o,$(filter %c,$(SRCS)))
# Make sure the kernel linked to correct address, no relocation!
all: $(HEX)
$(HEX): $(BIN)
objcopy --input-target=binary --output-target=verilog $(BIN) $(HEX)
$(BIN): $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) -o $@
%.o: %.c $(LISTS)
$(CC) $(CFLAGS) -l $(LISTS)/$<.list -c $< -o $@
%.o: %.s $(LISTS)
$(CC) $(CFLAGS) -l $(LISTS)/$<.list -c $< -o $@
$(LISTS):
mkdir -p $(addprefix $(LISTS)/,$(sort $(dir $(SRCS))))
.PHONY: clean
clean:
rm -rf $(OBJS) $(BIN) $(HEX) $(LISTS) $(NAME).map

View File

@@ -0,0 +1,35 @@
MEMORY
{
ZP: start = $0, size = $100, type = rw, define = yes;
SDRAM: start = $9200, size = $4d00, type = rw, define = yes;
ROM: start = $F000, size = $1000, file = %O;
}
SEGMENTS {
ZEROPAGE: load = ZP, type = zp, define = yes;
DATA: load = ROM, type = rw, define = yes, run = SDRAM;
BSS: load = SDRAM, type = bss, define = yes;
HEAP: load = SDRAM, type = bss, optional = yes;
STARTUP: load = ROM, type = ro;
ONCE: load = ROM, type = ro, optional = yes;
CODE: load = ROM, type = ro;
RODATA: load = ROM, type = ro;
VECTORS: load = ROM, type = ro, start = $FFFA;
}
FEATURES {
CONDES: segment = STARTUP,
type = constructor,
label = __CONSTRUCTOR_TABLE__,
count = __CONSTRUCTOR_COUNT__;
CONDES: segment = STARTUP,
type = destructor,
label = __DESTRUCTOR_TABLE__,
count = __DESTRUCTOR_COUNT__;
}
SYMBOLS {
# Define the stack size for the application
__STACKSIZE__: value = $0200, type = weak;
__STACKSTART__: type = weak, value = $0800; # 2k stack
}

View File

@@ -0,0 +1,35 @@
.export _init, nmi_int, irq_int
.autoimport
.import _init_interrupt_controller
.zeropage
finish: .res 1
.code
nmi_int:
irq_int:
lda #$6d
sta $00
_init:
ldx #$ff
txs
LDA #<(__STACKSTART__ + __STACKSIZE__)
STA sp
LDA #>(__STACKSTART__ + __STACKSIZE__)
STA sp+1
; enable interrupt 0
lda #$00
jsr pusha
lda #$1
jsr _enable_irq
cli
@end: bra @end

View File

@@ -0,0 +1,14 @@
; ---------------------------------------------------------------------------
; vectors.s
; ---------------------------------------------------------------------------
;
; Defines the interrupt vector table.
.import _init
.import nmi_int, irq_int
.segment "VECTORS"
.addr nmi_int ; NMI vector
.addr _init ; Reset vector
.addr irq_int ; IRQ/BRK vector