Add softuart to soc_cxxrtl test.

This commit is contained in:
Colin 2025-03-30 18:36:35 +08:00
parent 464bd40440
commit 2e649e2c86
5 changed files with 120 additions and 126 deletions

View File

@ -1,12 +1,23 @@
#include "tb_cxxrtl_io.h" #include "tb_cxxrtl_io.h"
__attribute__((optimize("O0"))) int main() { #include "uart.h"
#define CLK_FREQ_MHZ 12
// __attribute__((optimize("O0")))
int main() {
// Float stuff should all be compile-time. 115200 baud.
uart_init();
uart_clkdiv_baud(CLK_FREQ_MHZ, 9600);
uart_puts("Hello, UART!\n");
// Need to wait for completion, else we will terminate the simulation
// while characters still in FIFO.
uart_wait_done();
// tb_puts("Hello world from Hazard3 + CXXRTL!\n"); // tb_puts("Hello world from Hazard3 + CXXRTL!\n");
uint32_t addr = 0x40008000; uint32_t addr = 0x40008000;
uint32_t *point = (uint32_t *)addr; uint32_t *point = (uint32_t *)addr;
*point = 'C'; *point = 'C';
*point = 'O';
*point = 'L';
*point = 'I';
*point = 'N';
return 123; return 123;
} }

View File

@ -11,6 +11,9 @@ TBEXEC := $(patsubst %.f,%,$(DOTF))
FILE_LIST := $(shell HDL=$(HDL) $(SCRIPTS)/listfiles $(DOTF)) FILE_LIST := $(shell HDL=$(HDL) $(SCRIPTS)/listfiles $(DOTF))
BUILD_DIR := build-$(patsubst %.f,%,$(DOTF)) BUILD_DIR := build-$(patsubst %.f,%,$(DOTF))
SRC = tb.cpp softuart.c
INC = -I ${BUILD_DIR} -I ./
# Note: clang++-18 has a >20x compile time regression, even at low # Note: clang++-18 has a >20x compile time regression, even at low
# optimisation levels. I have tried clang++-16 and clang++-17, both fine. # optimisation levels. I have tried clang++-16 and clang++-17, both fine.
CLANGXX := clang++-16 CLANGXX := clang++-16
@ -40,7 +43,7 @@ gdb:
/opt/riscv/bin/riscv32-unknown-elf-gdb -x gdb_init /opt/riscv/bin/riscv32-unknown-elf-gdb -x gdb_init
$(TBEXEC): $(BUILD_DIR)/dut.cpp tb.cpp $(TBEXEC): $(BUILD_DIR)/dut.cpp tb.cpp
$(CLANGXX) -O3 -std=c++14 $(addprefix -D,$(CDEFINES) $(CDEFINES_$(DOTF))) -I $(shell yosys-config --datdir)/include/backends/cxxrtl/runtime -I $(BUILD_DIR) tb.cpp -o $(TBEXEC) $(CLANGXX) -O3 -std=c++14 $(addprefix -D,$(CDEFINES) $(CDEFINES_$(DOTF))) -I $(shell yosys-config --datdir)/include/backends/cxxrtl/runtime $(INC) ${SRC} -o $(TBEXEC)
lint: lint:
verilator --lint-only --top-module $(TOP) -I$(HDL) $(FILE_LIST) verilator --lint-only --top-module $(TOP) -I$(HDL) $(FILE_LIST)

View File

@ -15,100 +15,72 @@
#define SoftUart_IDEF_LEN_C2 (SoftUart_IDEF_LEN_C1 + SoftUart_STOP_Bit) #define SoftUart_IDEF_LEN_C2 (SoftUart_IDEF_LEN_C1 + SoftUart_STOP_Bit)
// All Soft Uart Config and State // All Soft Uart Config and State
SoftUart_S SUart[Number_Of_SoftUarts]; SoftUart_S SUart;
// TX RX Data Buffer // TX RX Data Buffer
SoftUartBuffer_S SUBuffer[Number_Of_SoftUarts]; SoftUartBuffer_S SUBuffer;
// For timing division // For timing division
__IO uint8_t SU_Timer = 0; uint8_t SU_Timer = 0;
// Parity var // Parity var
static uint8_t DV, PCount; static uint8_t DV, PCount;
// Read RX single Pin Value
GPIO_PinState SoftUartGpioReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin) {
return HAL_GPIO_ReadPin(GPIOx, GPIO_Pin);
}
// Write TX single Pin Value
void SoftUartGpioWritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin,
GPIO_PinState PinState) {
HAL_GPIO_WritePin(GPIOx, GPIO_Pin, PinState);
}
// Initial Soft Uart // Initial Soft Uart
SoftUartState_E SoftUartInit(uint8_t SoftUartNumber, GPIO_TypeDef *TxPort, SoftUart_S *SoftUartInit() {
uint16_t TxPin, GPIO_TypeDef *RxPort, SUart.TxNComplated = 0;
uint16_t RxPin) {
if (SoftUartNumber >= Number_Of_SoftUarts) return SoftUart_Error;
SUart[SoftUartNumber].TxNComplated = 0; SUart.RxBitCounter = 0;
SUart.RxBitShift = 0;
SUart.RxIndex = 0;
SUart[SoftUartNumber].RxBitCounter = 0; SUart.TxEnable = 0;
SUart[SoftUartNumber].RxBitShift = 0; SUart.RxEnable = 0;
SUart[SoftUartNumber].RxIndex = 0;
SUart[SoftUartNumber].TxEnable = 0; SUart.TxBitCounter = 0;
SUart[SoftUartNumber].RxEnable = 0; SUart.TxBitShift = 0;
SUart.TxIndex = 0;
SUart[SoftUartNumber].TxBitCounter = 0; SUart.TxSize = 0;
SUart[SoftUartNumber].TxBitShift = 0;
SUart[SoftUartNumber].TxIndex = 0;
SUart[SoftUartNumber].TxSize = 0; SUart.Buffer = &SUBuffer;
SUart[SoftUartNumber].Buffer = &SUBuffer[SoftUartNumber]; SUart.RxTimingFlag = 0;
SUart.RxBitOffset = 0;
SUart[SoftUartNumber].RxPort = RxPort; return &SUart;
SUart[SoftUartNumber].RxPin = RxPin;
SUart[SoftUartNumber].TxPort = TxPort;
SUart[SoftUartNumber].TxPin = TxPin;
SUart[SoftUartNumber].RxTimingFlag = 0;
SUart[SoftUartNumber].RxBitOffset = 0;
return SoftUart_OK;
} }
// Send one bit to TX pin // Send one bit to TX pin
void SoftUartTransmitBit(SoftUart_S *SU, uint8_t Bit0_1) { void SoftUartTransmitBit(SoftUart_S *SU, uint8_t Bit0_1) {
SoftUartGpioWritePin(SU->TxPort, SU->TxPin, (GPIO_PinState)Bit0_1); SU->TxPinValue = Bit0_1;
} }
// Enable Soft Uart Receiving // Enable Soft Uart Receiving
SoftUartState_E SoftUartEnableRx(uint8_t SoftUartNumber) { SoftUartState_E SoftUartEnableRx() {
if (SoftUartNumber >= Number_Of_SoftUarts) return SoftUart_Error; SUart.RxEnable = 1;
SUart[SoftUartNumber].RxEnable = 1;
return SoftUart_OK; return SoftUart_OK;
} }
// Disable Soft Uart Receiving // Disable Soft Uart Receiving
SoftUartState_E SoftUartDisableRx(uint8_t SoftUartNumber) { SoftUartState_E SoftUartDisableRx() {
if (SoftUartNumber >= Number_Of_SoftUarts) return SoftUart_Error; SUart.RxEnable = 0;
SUart[SoftUartNumber].RxEnable = 0;
return SoftUart_OK; return SoftUart_OK;
} }
// Read Size of Received Data in buffer // Read Size of Received Data in buffer
uint8_t SoftUartRxAlavailable(uint8_t SoftUartNumber) { uint8_t SoftUartRxAlavailable() { return SUart.RxIndex; }
return SUart[SoftUartNumber].RxIndex;
}
// Move Received Data to Another Buffer // Move Received Data to Another Buffer
SoftUartState_E SoftUartReadRxBuffer(uint8_t SoftUartNumber, uint8_t *Buffer, SoftUartState_E SoftUartReadRxBuffer(uint8_t *Buffer, uint8_t Len) {
uint8_t Len) {
int i; int i;
if (SoftUartNumber >= Number_Of_SoftUarts) return SoftUart_Error;
for (i = 0; i < Len; i++) { for (i = 0; i < Len; i++) {
Buffer[i] = SUart[SoftUartNumber].Buffer->Rx[i]; Buffer[i] = SUart.Buffer->Rx[i];
} }
for (i = 0; i < SUart[SoftUartNumber].RxIndex; i++) { for (i = 0; i < SUart.RxIndex; i++) {
SUart[SoftUartNumber].Buffer->Rx[i] = SUart.Buffer->Rx[i] = SUart.Buffer->Rx[i + Len];
SUart[SoftUartNumber].Buffer->Rx[i + Len];
} }
SUart[SoftUartNumber].RxIndex -= Len; SUart.RxIndex -= Len;
return SoftUart_OK; return SoftUart_OK;
} }
@ -212,78 +184,56 @@ void SoftUartRxDataBitProcess(SoftUart_S *SU, uint8_t B0_1) {
// Wait Until Transmit Completed // Wait Until Transmit Completed
// You do not usually need to use this function! // You do not usually need to use this function!
void SoftUartWaitUntilTxComplate(uint8_t SoftUartNumber) { void SoftUartWaitUntilTxComplate() { while (SUart.TxNComplated); }
while (SUart[SoftUartNumber].TxNComplated);
}
// Copy Data to Transmit Buffer and Start Sending // Copy Data to Transmit Buffer and Start Sending
SoftUartState_E SoftUartPuts(uint8_t SoftUartNumber, uint8_t *Data, SoftUartState_E SoftUartPuts(uint8_t *Data, uint8_t Len) {
uint8_t Len) {
int i; int i;
if (SUart.TxNComplated) return SoftUart_Error;
if (SoftUartNumber >= Number_Of_SoftUarts) return SoftUart_Error; SUart.TxIndex = 0;
if (SUart[SoftUartNumber].TxNComplated) return SoftUart_Error; SUart.TxSize = Len;
SUart[SoftUartNumber].TxIndex = 0;
SUart[SoftUartNumber].TxSize = Len;
for (i = 0; i < Len; i++) { for (i = 0; i < Len; i++) {
SUart[SoftUartNumber].Buffer->Tx[i] = Data[i]; SUart.Buffer->Tx[i] = Data[i];
} }
SUart.TxNComplated = 1;
SUart[SoftUartNumber].TxNComplated = 1; SUart.TxEnable = 1;
SUart[SoftUartNumber].TxEnable = 1;
return SoftUart_OK; return SoftUart_OK;
} }
// Capture RX and Get BitOffset // Capture RX and Get BitOffset
uint8_t SoftUartScanRxPorts(void) { uint8_t SoftUartScanRxPorts(SoftUart_S *SU) {
int i;
uint8_t Buffer = 0x00, Bit; uint8_t Buffer = 0x00, Bit;
for (i = 0; i < Number_Of_SoftUarts; i++) {
// Read RX GPIO Value // Read RX GPIO Value
Bit = SoftUartGpioReadPin(SUart[i].RxPort, SUart[i].RxPin); Bit = SU->RxPinValue;
// Starting conditions // Starting conditions
if (!SUart[i].RxBitCounter && !SUart[i].RxTimingFlag && !Bit) { if (!SUart.RxBitCounter && !SUart.RxTimingFlag && !Bit) {
// Save RX Bit Offset // Save RX Bit Offset
// Calculate middle position of data puls // Calculate middle position of data puls
SUart[i].RxBitOffset = ((SU_Timer + 2) % 5); SUart.RxBitOffset = ((SU_Timer + 2) % 5);
// Timing Offset is Set // Timing Offset is Set
SUart[i].RxTimingFlag = 1; SUart.RxTimingFlag = 1;
} }
// Add all RX GPIO State to Buffer // Add all RX GPIO State to Buffer
Buffer |= ((Bit & 0x01) << i); Buffer |= (Bit & 0x01);
}
return Buffer; return Buffer;
} }
// SoftUartHandler must call in interrupt every 0.2*(1/BR) // SoftUartHandler must call in interrupt every 0.2*(1/BR)
// if BR=9600 then 0.2*(1/9600)=20.8333333 uS // if BR=9600 then 0.2*(1/9600)=20.8333333 uS
void SoftUartHandler(void) { void SoftUartHandler(void) {
int i;
uint8_t SU_DBuffer; uint8_t SU_DBuffer;
// Capture RX and Get BitOffset // Capture RX and Get BitOffset
SU_DBuffer = SoftUartScanRxPorts(); SU_DBuffer = SoftUartScanRxPorts(&SUart);
for (i = 0; i < Number_Of_SoftUarts; i++) {
// Receive Data if we in middle data pulse position // Receive Data if we in middle data pulse position
if (SUart[i].RxBitOffset == SU_Timer) { if (SUart.RxBitOffset == SU_Timer) {
SoftUartRxDataBitProcess(&SUart[i], ((SU_DBuffer >> i) & 0x01)); SoftUartRxDataBitProcess(&SUart, (SU_DBuffer & 0x01));
}
} }
// Sending always happens in the first time slot // Sending always happens in the first time slot
if (SU_Timer == 0) { if (SU_Timer == 0) {
// Transmit Data // Transmit Data
for (i = 0; i < Number_Of_SoftUarts; i++) { SoftUartTxProcess(&SUart);
SoftUartTxProcess(&SUart[i]);
}
} }
// Timing process // Timing process

View File

@ -4,6 +4,11 @@
https://github.com/liyanboy74 https://github.com/liyanboy74
*/ */
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#define Number_Of_SoftUarts 1 // Max 8 #define Number_Of_SoftUarts 1 // Max 8
#define SoftUartTxBufferSize 32 #define SoftUartTxBufferSize 32
@ -21,7 +26,7 @@ typedef struct {
} SoftUartBuffer_S; } SoftUartBuffer_S;
typedef struct { typedef struct {
__IO uint8_t TxNComplated; uint8_t TxNComplated;
uint8_t TxEnable; uint8_t TxEnable;
uint8_t RxEnable; uint8_t RxEnable;
uint8_t TxBitShift, TxBitCounter; uint8_t TxBitShift, TxBitCounter;
@ -29,25 +34,19 @@ typedef struct {
uint8_t TxIndex, TxSize; uint8_t TxIndex, TxSize;
uint8_t RxIndex; uint8_t RxIndex;
SoftUartBuffer_S *Buffer; SoftUartBuffer_S *Buffer;
GPIO_TypeDef *TxPort;
uint16_t TxPin;
GPIO_TypeDef *RxPort;
uint16_t RxPin;
uint8_t RxTimingFlag; uint8_t RxTimingFlag;
uint8_t RxBitOffset; uint8_t RxBitOffset;
uint8_t RxPinValue; // set before SoftUartHandler
uint8_t TxPinValue; // get after SoftUartHandler
} SoftUart_S; } SoftUart_S;
// Call Every (0.2)*(1/9600) = 20.83 uS // Call Every (0.2)*(1/9600) = 20.83 uS
void SoftUartHandler(void); void SoftUartHandler(void);
void SoftUartWaitUntilTxComplate(uint8_t SoftUartNumber); void SoftUartWaitUntilTxComplate();
uint8_t SoftUartRxAlavailable(uint8_t SoftUartNumber); uint8_t SoftUartRxAlavailable();
SoftUartState_E SoftUartPuts(uint8_t SoftUartNumber, uint8_t *Data, SoftUartState_E SoftUartPuts(uint8_t *Data, uint8_t Len);
uint8_t Len); SoftUartState_E SoftUartEnableRx();
SoftUartState_E SoftUartEnableRx(uint8_t SoftUartNumber); SoftUartState_E SoftUartDisableRx();
SoftUartState_E SoftUartDisableRx(uint8_t SoftUartNumber); SoftUart_S *SoftUartInit();
SoftUartState_E SoftUartInit(uint8_t SoftUartNumber, GPIO_TypeDef *TxPort, SoftUartState_E SoftUartReadRxBuffer(uint8_t *Buffer, uint8_t Len);
uint16_t TxPin, GPIO_TypeDef *RxPort,
uint16_t RxPin);
SoftUartState_E SoftUartReadRxBuffer(uint8_t SoftUartNumber, uint8_t *Buffer,
uint8_t Len);

View File

@ -12,6 +12,7 @@
#include <cxxrtl/cxxrtl_vcd.h> #include <cxxrtl/cxxrtl_vcd.h>
#include "dut.cpp" #include "dut.cpp"
#include "softuart.h"
// There must be a better way // There must be a better way
#ifdef __x86_64__ #ifdef __x86_64__
@ -110,6 +111,9 @@ int main(int argc, char **argv) {
bool replay_jtag = false; bool replay_jtag = false;
std::string jtag_replay_path; std::string jtag_replay_path;
SoftUart_S *su = SoftUartInit();
SoftUartEnableRx();
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
std::string s(argv[i]); std::string s(argv[i]);
if (s.rfind("--", 0) != 0) { if (s.rfind("--", 0) != 0) {
@ -246,6 +250,8 @@ int main(int argc, char **argv) {
top.step(); top.step();
top.step(); // workaround for github.com/YosysHQ/yosys/issues/2780 top.step(); // workaround for github.com/YosysHQ/yosys/issues/2780
int uart_sample_count = 0;
bool timed_out = false; bool timed_out = false;
for (int64_t cycle = 0; cycle < max_cycles || max_cycles == 0; ++cycle) { for (int64_t cycle = 0; cycle < max_cycles || max_cycles == 0; ++cycle) {
top.p_clk.set<bool>(false); top.p_clk.set<bool>(false);
@ -335,6 +341,31 @@ int main(int argc, char **argv) {
} }
} }
// 12Mhz -> 9600 = 1250
// SoftUartHandler must call in interrupt every 0.2*(1/BR)
if (uart_sample_count >= (1250 / 5)) {
// if (top.p_uart__tx.get<bool>())
// printf("1");
// else
// printf("0");
su->RxPinValue = (top.p_uart__tx.get<bool>() ? 1 : 0);
SoftUartHandler();
// uint8_t tx = su->TxPinValue;
// uint8_t ch = getchar(0);
// SoftUartPuts(0, &ch, 1);
if (SoftUartRxAlavailable()) {
uint8_t ch = 0;
auto re = SoftUartReadRxBuffer(&ch, 1);
if (re == SoftUart_OK && ch) printf("%c", ch);
}
uart_sample_count = 0;
} else {
uart_sample_count++;
}
if (dump_waves) { if (dump_waves) {
// The extra step() is just here to get the bus responses to line up // The extra step() is just here to get the bus responses to line up
// nicely in the VCD (hopefully is a quick update) // nicely in the VCD (hopefully is a quick update)