Compare commits
25 Commits
d15e930649
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 55c6be3d00 | |||
| 23a4a69670 | |||
| b8d828c438 | |||
| 467ef32107 | |||
| 9edebe637b | |||
| dd51b5d610 | |||
| f0f26af791 | |||
| 724f8db1b1 | |||
| 269a04d5f5 | |||
| 2e640690ba | |||
| e7b38fb560 | |||
| e4936abb53 | |||
| 7f1f924331 | |||
| 0c7206f186 | |||
| 849cf7b7a1 | |||
| afba9e168f | |||
| 2e0507d0bb | |||
| d18d41fc02 | |||
| 6553fa04aa | |||
| 4ca569519e | |||
| e23cf79aa0 | |||
| af768cbb09 | |||
| 73edb77577 | |||
| d91ddeea22 | |||
| 252467c1be |
9
mbv/.clang-format
Normal file
9
mbv/.clang-format
Normal file
@@ -0,0 +1,9 @@
|
||||
---
|
||||
# We'll use defaults from the LLVM style, but with 4 columns indentation.
|
||||
BasedOnStyle: Google
|
||||
IndentWidth: 4
|
||||
---
|
||||
Language: Cpp
|
||||
# Force pointers to the type for C++.
|
||||
DerivePointerAlignment: false
|
||||
PointerAlignment: Left
|
||||
26
mbv/Makefile
26
mbv/Makefile
@@ -6,14 +6,18 @@ bootloader: ## Build the bootloader in docker
|
||||
helloworld: ## Build the helloworld app in docker
|
||||
docker build -o . --target export --build-arg TARGET=helloworld.bin .
|
||||
|
||||
.PHONY: wozmon
|
||||
wozmon: ## Build the wozmon app in docker
|
||||
docker build -o . --target export --build-arg TARGET=wozmon.bin .
|
||||
|
||||
.PHONY: timer
|
||||
timer: ## Build the timer app in docker
|
||||
docker build -o . --target export --build-arg TARGET=timer.bin .
|
||||
|
||||
.PHONY: uart
|
||||
uart: ## Build the uart app in docker
|
||||
docker build -o . --target export --build-arg TARGET=uart.bin .
|
||||
|
||||
.PHONY: async
|
||||
async: ## Build the async app in docker
|
||||
docker build -o . --target export --build-arg TARGET=async.bin .
|
||||
|
||||
.PHONY: dev-image
|
||||
dev-image:
|
||||
docker build -t mbv-dev --target dev .
|
||||
@@ -22,16 +26,20 @@ dev-image:
|
||||
dev: dev-image ## Run a dev container
|
||||
docker run -it --rm -v $(CURDIR):/workspace -w /workspace mbv-dev
|
||||
|
||||
.PHONY: precommit
|
||||
precommit: ## Make sure everything looks ok before pushing
|
||||
$(MAKE) bootloader
|
||||
$(MAKE) helloworld
|
||||
$(MAKE) timer
|
||||
$(MAKE) uart
|
||||
$(MAKE) async
|
||||
|
||||
.PHONY: clean
|
||||
clean: ## Remove generated files
|
||||
rm -rf *.elf *.bin $(all_objects) $(deps)
|
||||
rm -rf test/ *.dSYM $(test_deps) *.o
|
||||
rm -rf *.elf *.bin
|
||||
|
||||
.PHONY: help
|
||||
help: ## Show this help
|
||||
@echo Noteworthy targets:
|
||||
@egrep '^[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
|
||||
.DEFAULT_GOAL := help
|
||||
|
||||
#-include $(deps)
|
||||
#-include $(test_deps)
|
||||
|
||||
@@ -3,8 +3,6 @@ MEMORY
|
||||
RAM (rwx) : ORIGIN = 0x80000000, LENGTH = 0x10000000
|
||||
}
|
||||
|
||||
_vector_table = 0x0;
|
||||
|
||||
BiosUartRead = 0x0d4;
|
||||
BiosUartWrite = 0x22c;
|
||||
BiosWozmon = 0x340;
|
||||
@@ -23,10 +21,18 @@ SECTIONS
|
||||
*(.rodata*)
|
||||
} > RAM
|
||||
|
||||
.init :
|
||||
{
|
||||
__init_array_start = .;
|
||||
KEEP(*(.init_array*))
|
||||
__init_array_end = .;
|
||||
}
|
||||
|
||||
.bss (NOLOAD) :
|
||||
{
|
||||
_bss_begin = .;
|
||||
*(.bss*)
|
||||
*(.sbss*)
|
||||
*(COMMON)
|
||||
_bss_end = .;
|
||||
} > RAM
|
||||
@@ -34,10 +40,6 @@ SECTIONS
|
||||
.data :
|
||||
{
|
||||
*(.data*)
|
||||
|
||||
__exidx_start = .;
|
||||
*(.exidx*)
|
||||
__exidx_end = .;
|
||||
} > RAM
|
||||
|
||||
_heap_begin = .;
|
||||
|
||||
175
mbv/apps/async/main.cc
Normal file
175
mbv/apps/async/main.cc
Normal file
@@ -0,0 +1,175 @@
|
||||
#include "hal/gpio.h"
|
||||
#include "hal/intc.h"
|
||||
#include "hal/interrupts.h"
|
||||
#include "hal/pol0.h"
|
||||
#include "hal/timer.h"
|
||||
#include "lib/async.h"
|
||||
#include "lib/buffer.h"
|
||||
|
||||
#include "uart.h"
|
||||
#include "uart_async.h"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint32_t kTicksPerSecond = 100'000'000;
|
||||
constexpr uint32_t kTicksPerMicrosecond = 100;
|
||||
constexpr uint32_t kOverflowSeconds = (1ULL << 32) / kTicksPerSecond;
|
||||
constexpr uint32_t kOverflowMicroseconds =
|
||||
((1ULL << 32) % kTicksPerSecond) / kTicksPerMicrosecond;
|
||||
|
||||
Timer* timer0;
|
||||
volatile uint32_t t1overflowsecs = 0;
|
||||
volatile uint32_t t1overflowusecs = 0;
|
||||
|
||||
Gpio* gpio0;
|
||||
|
||||
void ToggleLed(int which) {
|
||||
uint8_t data = gpio0->data;
|
||||
data ^= (0x1 << which);
|
||||
gpio0->data = data;
|
||||
}
|
||||
|
||||
void SetLed(int which) {
|
||||
uint8_t data = gpio0->data;
|
||||
data |= (0x1 << which);
|
||||
gpio0->data = data;
|
||||
}
|
||||
|
||||
void ClearLed(int which) {
|
||||
uint8_t data = gpio0->data;
|
||||
data &= ~(0x1 << which);
|
||||
gpio0->data = data;
|
||||
}
|
||||
|
||||
void Uart0Isr() { HandleUartIsr(); }
|
||||
|
||||
void Timer0Isr() {
|
||||
if (timer0->HasT1Overflowed()) {
|
||||
UartWriteCrash("t1 overflow\r\n");
|
||||
t1overflowsecs += kOverflowSeconds;
|
||||
t1overflowusecs += kOverflowMicroseconds;
|
||||
timer0->ClearT1Overflow();
|
||||
return;
|
||||
}
|
||||
SetLed(6);
|
||||
UartWriteCrash("blarg\r\n");
|
||||
__builtin_trap();
|
||||
}
|
||||
|
||||
void SetupUart() {
|
||||
InitUarts();
|
||||
|
||||
intc::SetIsr(UART0_IRQN, Uart0Isr);
|
||||
intc::SetIrqEnabled(UART0_IRQN, true);
|
||||
}
|
||||
|
||||
void SetupTimer() {
|
||||
timer0 = Timer::Instance(TIMER0_BASE);
|
||||
timer0->SetupAsWdt(100'000 * 1000);
|
||||
timer0->EnableT1();
|
||||
|
||||
intc::SetIsr(TIMER0_IRQN, Timer0Isr);
|
||||
intc::SetIrqEnabled(TIMER0_IRQN, true);
|
||||
}
|
||||
|
||||
void SetupInterrupts() {
|
||||
intc::EnableInterrupts();
|
||||
SetExternalInterruptHandler(intc::InterruptHandler);
|
||||
EnableExternalInterrupts();
|
||||
EnableInterrupts(true);
|
||||
}
|
||||
|
||||
async::task<> echo() {
|
||||
async::task<std::byte> reader = UartReadLoop();
|
||||
async::gimme<std::span<const std::byte>> feeder;
|
||||
async::task<> writer = UartWriteLoop(feeder);
|
||||
writer.h.resume(); // advance to first yield
|
||||
while (1) {
|
||||
SetLed(1);
|
||||
std::byte c = co_await reader;
|
||||
ClearLed(1);
|
||||
co_await feeder.feed(std::span{&c, 1});
|
||||
}
|
||||
}
|
||||
|
||||
async::task<> blink() {
|
||||
while (1) {
|
||||
co_await async::delay(500);
|
||||
ToggleLed(0);
|
||||
timer0->Pet();
|
||||
|
||||
co_await UartWrite(".");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
gpio0 = Gpio::Instance(GPIO0_BASE);
|
||||
gpio0->data = 0;
|
||||
|
||||
SetupUart();
|
||||
UartWriteBlocking("uart setup done\r\n");
|
||||
SetupTimer();
|
||||
UartWriteBlocking("timer setup done\r\n");
|
||||
|
||||
SetupInterrupts();
|
||||
|
||||
auto e = echo();
|
||||
auto b = blink();
|
||||
async::schedule(e.h);
|
||||
async::schedule(b.h);
|
||||
|
||||
UartWriteBlocking("init done. starting main loop\r\n");
|
||||
|
||||
async::main_loop([]() {
|
||||
return false;
|
||||
});
|
||||
// should never get here
|
||||
}
|
||||
|
||||
/// stdlib stuff
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "lib/lock.h"
|
||||
|
||||
extern unsigned char _heap_begin, _heap_end;
|
||||
|
||||
extern "C" void* _sbrk(int increment) {
|
||||
static unsigned char* heap = &_heap_begin;
|
||||
unsigned char* prev_heap = heap;
|
||||
if (heap + increment >= &_heap_end) {
|
||||
UartWriteCrash("Heap overflow!\r\n");
|
||||
return reinterpret_cast<void*>(-1);
|
||||
}
|
||||
heap += increment;
|
||||
return prev_heap;
|
||||
}
|
||||
|
||||
extern "C" int _gettimeofday(struct timeval* tv, void* tzvp) {
|
||||
(void)tzvp;
|
||||
uint32_t ticks = timer0->GetT1Ticks();
|
||||
tv->tv_sec = ticks / kTicksPerSecond + t1overflowsecs;
|
||||
tv->tv_usec =
|
||||
(ticks % kTicksPerSecond) / kTicksPerMicrosecond + t1overflowusecs;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" uint8_t __atomic_exchange_1(volatile void* ptr, uint8_t val,
|
||||
int memorder) {
|
||||
(void)memorder;
|
||||
auto* dest = reinterpret_cast<volatile uint8_t*>(ptr);
|
||||
bool ret;
|
||||
|
||||
{
|
||||
InterruptLock lock;
|
||||
ret = *dest;
|
||||
*dest = val;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
166
mbv/apps/async/uart.cc
Normal file
166
mbv/apps/async/uart.cc
Normal file
@@ -0,0 +1,166 @@
|
||||
#include "uart.h"
|
||||
|
||||
#include "hal/gpio.h"
|
||||
#include "hal/pol0.h"
|
||||
#include "lib/async.h"
|
||||
#include "lib/lock.h"
|
||||
#include "lib/ring_buffer.h"
|
||||
#include "xuartlite.h"
|
||||
|
||||
#include "uart_async.h"
|
||||
|
||||
namespace {
|
||||
using async::AwaitableType;
|
||||
|
||||
constexpr uintptr_t kUart0BaseAddress = UART0_BASE;
|
||||
XUartLite uart0_inst;
|
||||
XUartLite_Config uart0_config = {
|
||||
.DeviceId = 0,
|
||||
.RegBaseAddr = kUart0BaseAddress,
|
||||
.BaudRate = 115200,
|
||||
.UseParity = false,
|
||||
.DataBits = 8,
|
||||
};
|
||||
|
||||
constexpr size_t kUartRxBufferSize = 256;
|
||||
std::array<std::byte, kUartRxBufferSize> rx_buffer = {};
|
||||
RingBuffer rx_ring_buffer{.buffer = rx_buffer};
|
||||
|
||||
constexpr size_t kUartTxBufferSize = 256;
|
||||
std::array<std::byte, kUartTxBufferSize> tx_buffer = {};
|
||||
RingBuffer tx_ring_buffer{.buffer = tx_buffer};
|
||||
|
||||
XUartLite* uart0 = &uart0_inst;
|
||||
|
||||
volatile bool sending;
|
||||
|
||||
void StartReceiving() {
|
||||
while (1) {
|
||||
if (rx_ring_buffer.FreeSpace() < 1) {
|
||||
// woops, full. discard some data
|
||||
// TODO: keep track of overrun stats
|
||||
rx_ring_buffer.Pop(1);
|
||||
}
|
||||
if (XUartLite_Recv(uart0, rx_ring_buffer.RawWritePointer(), 1) < 1) {
|
||||
break;
|
||||
}
|
||||
rx_ring_buffer.Push(1);
|
||||
}
|
||||
}
|
||||
|
||||
void StartSending() {
|
||||
if (sending) {
|
||||
return;
|
||||
}
|
||||
size_t tosend = tx_ring_buffer.ContiguousAvailableData();
|
||||
if (tosend < 1) {
|
||||
return;
|
||||
}
|
||||
sending = true;
|
||||
XUartLite_Send(uart0, tx_ring_buffer.RawReadPointer(), tosend);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void InitUarts() {
|
||||
XUartLite_CfgInitialize(uart0, &uart0_config, uart0_config.RegBaseAddr);
|
||||
|
||||
XUartLite_SetSendHandler(uart0, HandleUartTxFromIsr, nullptr);
|
||||
XUartLite_SetRecvHandler(uart0, HandleUartRxFromIsr, nullptr);
|
||||
StartReceiving();
|
||||
XUartLite_EnableInterrupt(uart0);
|
||||
|
||||
sending = false;
|
||||
}
|
||||
|
||||
void UartWriteCrash(std::span<const std::byte> data) {
|
||||
XUartLite_DisableInterrupt(uart0);
|
||||
while (data.size() > 0) {
|
||||
while (XUartLite_IsSending(uart0)) {
|
||||
}
|
||||
auto* dat =
|
||||
reinterpret_cast<uint8_t*>(const_cast<std::byte*>(data.data()));
|
||||
uint8_t sent = XUartLite_Send(uart0, dat, data.size());
|
||||
data = data.subspan(sent);
|
||||
}
|
||||
while (XUartLite_IsSending(uart0)) {
|
||||
}
|
||||
XUartLite_Send(uart0, nullptr,
|
||||
0); // reset buffer before enabling interrupts
|
||||
XUartLite_EnableInterrupt(uart0);
|
||||
}
|
||||
|
||||
async::task<> UartWrite(std::span<const std::byte> data) {
|
||||
while (!tx_ring_buffer.Store(data)) {
|
||||
co_await async::await(AwaitableType::kUartTx);
|
||||
}
|
||||
|
||||
{
|
||||
InterruptLock lock;
|
||||
StartSending();
|
||||
}
|
||||
}
|
||||
|
||||
async::task<> UartWriteLoop(
|
||||
async::gimme<std::span<const std::byte>>& data_gen) {
|
||||
while (1) {
|
||||
auto data = co_await data_gen;
|
||||
while (!tx_ring_buffer.Store(data)) {
|
||||
co_await async::await(AwaitableType::kUartTx);
|
||||
}
|
||||
|
||||
{
|
||||
InterruptLock lock;
|
||||
StartSending();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: use chunks to allow receiving more than 256 bytes at once
|
||||
void UartReadBlocking(std::span<std::byte> data) {
|
||||
while (!rx_ring_buffer.Load(data)) {
|
||||
}
|
||||
}
|
||||
|
||||
void UartWriteBlocking(std::span<const std::byte> data) {
|
||||
while (!tx_ring_buffer.Store(data)) {
|
||||
}
|
||||
|
||||
{
|
||||
InterruptLock lock;
|
||||
StartSending();
|
||||
}
|
||||
}
|
||||
|
||||
async::task<std::byte> UartReadLoop() {
|
||||
std::byte c;
|
||||
while (1) {
|
||||
while (!rx_ring_buffer.Load(std::span{&c, 1})) {
|
||||
co_await async::await(AwaitableType::kUartRx);
|
||||
}
|
||||
|
||||
co_yield c;
|
||||
}
|
||||
}
|
||||
|
||||
async::task<buffer> UartRead(int size) {
|
||||
auto buff = buffer::make(size);
|
||||
while (!rx_ring_buffer.Load(buff.data)) {
|
||||
co_await async::await(AwaitableType::kUartRx);
|
||||
}
|
||||
co_return buff;
|
||||
}
|
||||
|
||||
void HandleUartTxFromIsr(void*, unsigned int transmitted) {
|
||||
sending = false;
|
||||
tx_ring_buffer.Pop(transmitted);
|
||||
StartSending();
|
||||
async::resume(AwaitableType::kUartTx);
|
||||
}
|
||||
|
||||
void HandleUartRxFromIsr(void*, unsigned int transmitted) {
|
||||
rx_ring_buffer.Push(transmitted);
|
||||
StartReceiving();
|
||||
async::resume(AwaitableType::kUartRx);
|
||||
}
|
||||
|
||||
void HandleUartIsr() { XUartLite_InterruptHandler(uart0); }
|
||||
35
mbv/apps/async/uart.h
Normal file
35
mbv/apps/async/uart.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
void InitUarts();
|
||||
|
||||
// block until the provided buffer is full
|
||||
void UartReadBlocking(std::span<std::byte> data);
|
||||
inline uint8_t UartReadByteBlocking() {
|
||||
std::byte byte;
|
||||
UartReadBlocking(std::span{&byte, 1});
|
||||
return static_cast<uint8_t>(byte);
|
||||
}
|
||||
|
||||
// send and poll the uart until transmitted
|
||||
void UartWriteCrash(std::span<const std::byte> data);
|
||||
inline void UartWriteCrash(std::string_view s) {
|
||||
return UartWriteCrash(std::as_bytes(std::span{s.data(), s.size()}));
|
||||
}
|
||||
|
||||
// block until room is available in tx fifo, then send
|
||||
void UartWriteBlocking(std::span<const std::byte> data);
|
||||
inline void UartWriteBlocking(std::string_view s) {
|
||||
return UartWriteBlocking(std::as_bytes(std::span{s.data(), s.size()}));
|
||||
}
|
||||
|
||||
void HandleUartTxFromIsr(void*, unsigned int transmitted);
|
||||
void HandleUartRxFromIsr(void*, unsigned int);
|
||||
void HandleUartIsr();
|
||||
|
||||
uint8_t UartStatus();
|
||||
|
||||
void LogStuff();
|
||||
15
mbv/apps/async/uart_async.h
Normal file
15
mbv/apps/async/uart_async.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
#include "lib/async.h"
|
||||
#include "lib/buffer.h"
|
||||
|
||||
async::task<buffer> UartRead(int size);
|
||||
async::task<std::byte> UartReadLoop();
|
||||
async::task<> UartWrite(std::span<const std::byte> data);
|
||||
inline async::task<> UartWrite(std::string_view s) {
|
||||
co_await UartWrite(std::as_bytes(std::span{s.data(), s.size()}));
|
||||
}
|
||||
async::task<> UartWriteLoop(async::gimme<std::span<const std::byte>>& data);
|
||||
@@ -1,22 +1,24 @@
|
||||
#include <cstdint>
|
||||
|
||||
#include "pol0.h"
|
||||
#include "hal/gpio.h"
|
||||
#include "hal/pol0.h"
|
||||
|
||||
struct Gpio {
|
||||
volatile uint32_t data;
|
||||
};
|
||||
namespace {
|
||||
|
||||
#define gpio0 ((Gpio*)GPIO0_BASE)
|
||||
Gpio* gpio0;
|
||||
|
||||
void sleep(int ms) {
|
||||
for (int m = 0; m < ms; m++) {
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
asm volatile ( "" );
|
||||
asm volatile("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
gpio0 = Gpio::Instance(GPIO0_BASE);
|
||||
int out = 0;
|
||||
|
||||
while (1) {
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
#include <cstdint>
|
||||
|
||||
#include "intc.h"
|
||||
#include "interrupts.h"
|
||||
#include "pol0.h"
|
||||
#include "timer.h"
|
||||
#include "hal/bios.h"
|
||||
#include "hal/gpio.h"
|
||||
#include "hal/intc.h"
|
||||
#include "hal/interrupts.h"
|
||||
#include "hal/pol0.h"
|
||||
#include "hal/timer.h"
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
struct Gpio {
|
||||
volatile uint32_t data;
|
||||
};
|
||||
|
||||
Gpio* leds = reinterpret_cast<Gpio*>(GPIO0_BASE);
|
||||
Gpio* leds;
|
||||
Timer* timer;
|
||||
|
||||
}
|
||||
|
||||
void Timer0Isr() {
|
||||
static int counter = 0;
|
||||
leds->data = counter++;
|
||||
@@ -29,17 +26,21 @@ void SetupTimer() {
|
||||
timer->SetupAsWdt(100'000'000);
|
||||
timer->EnableT1();
|
||||
|
||||
SetIsr(TIMER0_IRQN, Timer0Isr);
|
||||
SetIrqEnabled(TIMER0_IRQN, true);
|
||||
EnableInterrupts();
|
||||
intc::SetIsr(TIMER0_IRQN, Timer0Isr);
|
||||
intc::SetIrqEnabled(TIMER0_IRQN, true);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
leds = Gpio::Instance(GPIO0_BASE);
|
||||
leds->data = 0xa0;
|
||||
|
||||
SetupTimer();
|
||||
SetExternalInterruptHandler(InterruptHandler);
|
||||
intc::EnableInterrupts();
|
||||
SetExternalInterruptHandler(intc::InterruptHandler);
|
||||
EnableExternalInterrupts();
|
||||
EnableInterrupts(true);
|
||||
|
||||
leds->data = 0xa1;
|
||||
|
||||
|
||||
80
mbv/apps/uart/uart.cc
Normal file
80
mbv/apps/uart/uart.cc
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <cstdint>
|
||||
|
||||
#include "hal/gpio.h"
|
||||
#include "hal/intc.h"
|
||||
#include "hal/interrupts.h"
|
||||
#include "hal/pol0.h"
|
||||
#include "xuartlite.h"
|
||||
|
||||
namespace {
|
||||
|
||||
Gpio* leds;
|
||||
|
||||
XUartLite uart0_inst;
|
||||
XUartLite_Config uart0_config = {
|
||||
.DeviceId = 0,
|
||||
.RegBaseAddr = UART0_BASE,
|
||||
.BaudRate = 115200,
|
||||
.UseParity = false,
|
||||
.DataBits = 8,
|
||||
};
|
||||
|
||||
XUartLite* uart0 = &uart0_inst;
|
||||
|
||||
volatile int incoming = 0;
|
||||
|
||||
void HandleUartRxFromIsr(void*, unsigned int) { incoming += 1; }
|
||||
|
||||
void HandleUartTxFromIsr(void*, unsigned int) {}
|
||||
|
||||
void Uart0Isr() { XUartLite_InterruptHandler(uart0); }
|
||||
|
||||
void InitUarts() {
|
||||
XUartLite_CfgInitialize(uart0, &uart0_config, uart0_config.RegBaseAddr);
|
||||
|
||||
XUartLite_SetSendHandler(uart0, HandleUartTxFromIsr, nullptr);
|
||||
XUartLite_SetRecvHandler(uart0, HandleUartRxFromIsr, nullptr);
|
||||
XUartLite_EnableInterrupt(uart0);
|
||||
|
||||
intc::SetIsr(UART0_IRQN, Uart0Isr);
|
||||
intc::SetIrqEnabled(UART0_IRQN, true);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
leds = Gpio::Instance(GPIO0_BASE);
|
||||
leds->data = 0xa0;
|
||||
|
||||
InitUarts();
|
||||
intc::EnableInterrupts();
|
||||
SetExternalInterruptHandler(intc::InterruptHandler);
|
||||
EnableExternalInterrupts();
|
||||
EnableInterrupts(true);
|
||||
|
||||
int counter = 0;
|
||||
|
||||
uint8_t c;
|
||||
while (XUartLite_Recv(uart0, &c, 1) > 0) {
|
||||
XUartLite_Send(uart0, &c, 1);
|
||||
while (XUartLite_IsSending(uart0)) {
|
||||
}
|
||||
}
|
||||
leds->data = 0xa1;
|
||||
while (1) {
|
||||
// should disable interrupts around this
|
||||
if (incoming > 0) {
|
||||
counter += 1;
|
||||
leds->data = counter;
|
||||
XUartLite_Send(uart0, &c, 1);
|
||||
while (XUartLite_IsSending(uart0)) {
|
||||
}
|
||||
while (XUartLite_Recv(uart0, &c, 1) > 0) {
|
||||
XUartLite_Send(uart0, &c, 1);
|
||||
while (XUartLite_IsSending(uart0)) {
|
||||
}
|
||||
}
|
||||
incoming -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,168 +0,0 @@
|
||||
#include <cstdint>
|
||||
|
||||
#include "xuartlite.h"
|
||||
|
||||
struct Gpio {
|
||||
volatile uint32_t data;
|
||||
};
|
||||
|
||||
#define gpio0 ((Gpio*)0x40000000)
|
||||
|
||||
namespace {
|
||||
constexpr uintptr_t kUart0BaseAddress = 0x40600000;
|
||||
XUartLite uart0_inst;
|
||||
XUartLite_Config uart0_config = {
|
||||
.DeviceId = 0,
|
||||
.RegBaseAddr = kUart0BaseAddress,
|
||||
.BaudRate = 115200,
|
||||
.UseParity = false,
|
||||
.DataBits = 8,
|
||||
};
|
||||
|
||||
XUartLite* uart0 = &uart0_inst;
|
||||
|
||||
void InitUarts() {
|
||||
XUartLite_CfgInitialize(uart0, &uart0_config, uart0_config.RegBaseAddr);
|
||||
}
|
||||
|
||||
uint8_t UartRead() {
|
||||
uint8_t c;
|
||||
while (XUartLite_Recv(uart0, &c, 1) < 1) {
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
void UartWrite(uint8_t c) {
|
||||
XUartLite_Send(uart0, &c, 1);
|
||||
while (XUartLite_IsSending(uart0)) {}
|
||||
}
|
||||
|
||||
void Jump(uint32_t addr) {
|
||||
auto jump = reinterpret_cast<void (*)()>(addr);
|
||||
jump();
|
||||
}
|
||||
|
||||
constexpr uint8_t kBackspace = 0x7f;
|
||||
constexpr uint8_t kOtherBackspace = 0x08;
|
||||
|
||||
uint8_t ReadHexNibble(uint8_t c) {
|
||||
// lowercase only
|
||||
if (c <= '9') {
|
||||
return c - '0';
|
||||
}
|
||||
return 10 + (c - 'a');
|
||||
}
|
||||
|
||||
uint32_t ReadHex(const char* buf) {
|
||||
uint32_t out = 0;
|
||||
while (*buf == ' ') {
|
||||
buf++;
|
||||
}
|
||||
for (int i = 0; i < 8; i++) {
|
||||
uint8_t c = ReadHexNibble(*buf);
|
||||
if (c > 0xf) {
|
||||
break;
|
||||
}
|
||||
out = (out << 4) + c;
|
||||
buf++;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void WriteHexNibble(uint8_t c) {
|
||||
if (c > 9) {
|
||||
UartWrite('a' + c - 10);
|
||||
} else {
|
||||
UartWrite('0' + c);
|
||||
}
|
||||
}
|
||||
|
||||
void UartWriteUint32(uint32_t a) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
WriteHexNibble((a >> 28) & 0xf);
|
||||
a <<= 4;
|
||||
}
|
||||
}
|
||||
|
||||
void UartWriteUint8(uint8_t a) {
|
||||
WriteHexNibble(a >> 4);
|
||||
WriteHexNibble(a & 0xf);
|
||||
}
|
||||
|
||||
void UartDump(uint32_t addr, int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
UartWrite(' ');
|
||||
UartWriteUint8(*reinterpret_cast<uint8_t*>(addr + i));
|
||||
}
|
||||
}
|
||||
|
||||
void DumpHex(uint32_t addr) {
|
||||
addr &= 0xfffffffc;
|
||||
UartWriteUint32(addr);
|
||||
UartWrite(':');
|
||||
UartDump(addr, 4);
|
||||
UartWrite('\r');
|
||||
UartWrite('\n');
|
||||
}
|
||||
|
||||
int FindChar(const char* buf, uint8_t c) {
|
||||
int found = 0;
|
||||
while (*buf) {
|
||||
if (*buf == c) {
|
||||
return found;
|
||||
}
|
||||
found++;
|
||||
buf++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
gpio0->data = 1;
|
||||
uint32_t cur_addr = 0;
|
||||
uint32_t cur_data = 0;
|
||||
|
||||
char inbuf[64] = {};
|
||||
char* inptr = inbuf;
|
||||
|
||||
InitUarts();
|
||||
|
||||
while (1) {
|
||||
uint8_t c = UartRead();
|
||||
UartWrite(c); // echo
|
||||
if (c == '\r') {
|
||||
gpio0->data = 0x55;
|
||||
*inptr = 0;
|
||||
if (inptr == inbuf) {
|
||||
cur_addr += 4;
|
||||
} else if (FindChar(inbuf, 'r') >= 0) {
|
||||
Jump(cur_addr);
|
||||
} else {
|
||||
cur_addr = ReadHex(inbuf);
|
||||
UartWrite('\n');
|
||||
}
|
||||
DumpHex(cur_addr);
|
||||
int assigned = FindChar(inbuf, ':');
|
||||
if (assigned >= 0) {
|
||||
cur_data = ReadHex(inbuf + assigned + 1);
|
||||
*(reinterpret_cast<uint32_t*>(cur_addr)) = cur_data;
|
||||
}
|
||||
inptr = inbuf;
|
||||
} else if (c == kBackspace) {
|
||||
inptr--;
|
||||
if (inptr < inbuf) {
|
||||
inptr = inbuf;
|
||||
continue;
|
||||
}
|
||||
UartWrite(kOtherBackspace);
|
||||
UartWrite(' ');
|
||||
UartWrite(kOtherBackspace);
|
||||
} else {
|
||||
*inptr++ = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <cstdint>
|
||||
|
||||
#include "pol0.h"
|
||||
#include "hal/pol0.h"
|
||||
#include "xuartlite.h"
|
||||
|
||||
uint8_t UartRead();
|
||||
@@ -52,7 +52,8 @@ uint8_t UartRead() {
|
||||
|
||||
void UartWrite(uint8_t c) {
|
||||
XUartLite_Send(uart0, &c, 1);
|
||||
while (XUartLite_IsSending(uart0)) {}
|
||||
while (XUartLite_IsSending(uart0)) {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
@@ -24,10 +24,6 @@ SECTIONS
|
||||
.data :
|
||||
{
|
||||
*(.data*)
|
||||
|
||||
__exidx_start = .;
|
||||
*(.exidx*)
|
||||
__exidx_end = .;
|
||||
} > BLRAM
|
||||
|
||||
_initial_stack_pointer = 0x1000;
|
||||
|
||||
@@ -59,10 +59,10 @@ void UartWriteUint8(uint8_t a) {
|
||||
}
|
||||
|
||||
void UartDump(uint32_t addr, int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
UartWrite(' ');
|
||||
UartWriteUint8(*reinterpret_cast<uint8_t*>(addr + i));
|
||||
}
|
||||
for (int i = 0; i < count; i++) {
|
||||
UartWrite(' ');
|
||||
UartWriteUint8(*reinterpret_cast<uint8_t*>(addr + i));
|
||||
}
|
||||
}
|
||||
|
||||
void DumpHex(uint32_t addr) {
|
||||
@@ -88,8 +88,7 @@ int FindChar(const char* buf, uint8_t c) {
|
||||
|
||||
} // namespace
|
||||
|
||||
__attribute__((used))
|
||||
void wozmon() {
|
||||
__attribute__((used)) void wozmon() {
|
||||
uint32_t cur_addr = 0;
|
||||
uint32_t cur_data = 0;
|
||||
|
||||
@@ -98,7 +97,7 @@ void wozmon() {
|
||||
|
||||
while (1) {
|
||||
uint8_t c = UartRead();
|
||||
UartWrite(c); // echo
|
||||
UartWrite(c); // echo
|
||||
if (c == '\r') {
|
||||
*inptr = 0;
|
||||
if (inptr == inbuf) {
|
||||
|
||||
266
mbv/configure
vendored
266
mbv/configure
vendored
@@ -2,84 +2,93 @@
|
||||
|
||||
import argparse
|
||||
import collections
|
||||
import dataclasses
|
||||
import glob
|
||||
import itertools
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
# global config
|
||||
toolchain_path = f"/opt/xpack-riscv-none-elf-gcc-14.2.0-3"
|
||||
toolchain_prefix = "riscv-none-elf-"
|
||||
builddir = "build"
|
||||
outdir = "out"
|
||||
linker_script = "apps/app.ld"
|
||||
hostcflags = "-g -std=c++20 -fprofile-instr-generate -fcoverage-mapping"
|
||||
hostlibs = "-lgtest -lgmock -lgtest_main"
|
||||
hostldflags = "-fprofile-instr-generate -fcoverage-mapping"
|
||||
include_dirs = [
|
||||
"hal",
|
||||
"hal/uart",
|
||||
"hal/lib/common",
|
||||
]
|
||||
class Config:
|
||||
def __init__(self, builddir, outdir):
|
||||
self.builddir = builddir
|
||||
self.outdir = outdir
|
||||
|
||||
include_flags = [f"-I{os.path.relpath(i, builddir)}" for i in include_dirs]
|
||||
project_flags = [
|
||||
]
|
||||
cpp_flags = ["-DNDEBUG"] + include_flags + project_flags
|
||||
common_flags = [
|
||||
"-g",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-flto",
|
||||
"-march=rv32i_zicsr",
|
||||
"-ffunction-sections",
|
||||
"-Oz",
|
||||
]
|
||||
if not os.path.exists(builddir):
|
||||
os.mkdir(builddir)
|
||||
if not os.path.exists(outdir):
|
||||
os.mkdir(outdir)
|
||||
|
||||
cc_flags = common_flags
|
||||
cxx_flags = common_flags + ["-std=c++20", "-fno-rtti", "-fno-exceptions", "-Wno-missing-field-initializers"]
|
||||
ldflags = [
|
||||
"-Oz",
|
||||
"-g",
|
||||
"-Wl,--gc-sections",
|
||||
"-Wl,--print-memory-usage",
|
||||
"-flto",
|
||||
"-march=rv32i_zicsr",
|
||||
]
|
||||
toolchain_path = f"/opt/xpack-riscv-none-elf-gcc-14.2.0-3"
|
||||
toolchain_prefix = "riscv-none-elf-"
|
||||
hostcflags = "-g -std=c++20 -fprofile-instr-generate -fcoverage-mapping"
|
||||
hostlibs = "-lgtest -lgmock -lgtest_main"
|
||||
hostldflags = "-fprofile-instr-generate -fcoverage-mapping"
|
||||
include_dirs = [
|
||||
".",
|
||||
"hal/uart",
|
||||
"hal/xilinx",
|
||||
]
|
||||
common_flags = [
|
||||
"-g",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-flto",
|
||||
"-march=rv32i_zicsr",
|
||||
"-ffunction-sections",
|
||||
"-Oz",
|
||||
]
|
||||
ldflags = [
|
||||
"-Oz",
|
||||
"-g",
|
||||
"-Wl,--gc-sections",
|
||||
"-Wl,--print-memory-usage",
|
||||
"-specs=nano.specs",
|
||||
"-flto",
|
||||
"-march=rv32i_zicsr",
|
||||
]
|
||||
project_flags = [
|
||||
]
|
||||
|
||||
@property
|
||||
def include_flags(self):
|
||||
return [f"-I{os.path.relpath(i, self.builddir)}" for i in self.include_dirs]
|
||||
|
||||
def get_cxx_flags():
|
||||
return cpp_flags + cxx_flags
|
||||
@property
|
||||
def cpp_flags(self):
|
||||
return ["-DNDEBUG"] + self.include_flags + self.project_flags
|
||||
|
||||
@property
|
||||
def cc_flags(self):
|
||||
return self.common_flags + self.cpp_flags
|
||||
|
||||
def get_cc_flags():
|
||||
return cpp_flags + cc_flags
|
||||
@property
|
||||
def cxx_flags(self):
|
||||
return self.common_flags + [
|
||||
"-std=c++20",
|
||||
"-fno-rtti",
|
||||
"-fno-exceptions",
|
||||
"-Wno-missing-field-initializers",
|
||||
] + self.cpp_flags
|
||||
|
||||
def relbuild(self, path):
|
||||
return os.path.relpath(path, self.builddir)
|
||||
|
||||
def get_ldflags():
|
||||
return ldflags
|
||||
|
||||
|
||||
def add_cpp_flag(flag):
|
||||
cpp_flags.append(flag)
|
||||
|
||||
|
||||
def gen_rules():
|
||||
def gen_rules(config):
|
||||
tools = {"cxx": "g++", "cc": "gcc", "as": "as", "objcopy": "objcopy"}
|
||||
|
||||
tc_path = toolchain_path
|
||||
tc_prefix = toolchain_prefix
|
||||
tc_path = config.toolchain_path
|
||||
tc_prefix = config.toolchain_prefix
|
||||
|
||||
rules = f"""
|
||||
rule cxx
|
||||
command = $cxx -MMD -MT $out -MF $out.d {' '.join(get_cxx_flags())} -c $in -o $out
|
||||
command = $cxx -MMD -MT $out -MF $out.d {' '.join(config.cxx_flags)} -c $in -o $out
|
||||
description = CXX $out
|
||||
depfile = $out.d
|
||||
deps = gcc
|
||||
|
||||
rule cc
|
||||
command = $cc -MMD -MT $out -MF $out.d {' '.join(get_cc_flags())} -c $in -o $out
|
||||
command = $cc -MMD -MT $out -MF $out.d {' '.join(config.cc_flags)} -c $in -o $out
|
||||
description = CC $out
|
||||
depfile = $out.d
|
||||
deps = gcc
|
||||
@@ -89,7 +98,7 @@ rule as
|
||||
description = AS $out
|
||||
|
||||
rule link
|
||||
command = $cxx {' '.join(ldflags)} -Wl,-T$linker_script -o $out $in $libs
|
||||
command = $cxx {' '.join(config.ldflags)} -Wl,-T$linker_script -o $out $in $libs
|
||||
description = LINK $out
|
||||
|
||||
rule objcopy
|
||||
@@ -97,13 +106,13 @@ rule objcopy
|
||||
description = OBJCOPY $out
|
||||
|
||||
rule hostcxx
|
||||
command = clang++ -MMD -MT $out -MF $out.d {hostcflags} -c $in -o $out
|
||||
command = clang++ -MMD -MT $out -MF $out.d {config.hostcflags} -c $in -o $out
|
||||
description = HOSTCXX $out
|
||||
depfile = $out.d
|
||||
deps = gcc
|
||||
|
||||
rule hostlink
|
||||
command = clang++ {hostldflags} -o $out $in {hostlibs}
|
||||
command = clang++ {config.hostldflags} -o $out $in {config.hostlibs}
|
||||
description = HOSTLINK $out
|
||||
|
||||
rule profdata
|
||||
@@ -140,7 +149,7 @@ def get_suffix_rule(filename, cxx_rule="cxx", cc_rule="cc"):
|
||||
|
||||
|
||||
def make_cxx_rule(name, cflags=()):
|
||||
cflags = " ".join(cflags + get_cxx_flags())
|
||||
cflags = " ".join(cflags)
|
||||
rule = f"""
|
||||
rule {name}
|
||||
command = $cxx -MMD -MT $out -MF $out.d {cflags} -c $in -o $out
|
||||
@@ -152,7 +161,7 @@ rule {name}
|
||||
|
||||
|
||||
def make_cc_rule(name, cflags=()):
|
||||
cflags = " ".join(cflags + get_cc_flags())
|
||||
cflags = " ".join(cflags)
|
||||
rule = f"""
|
||||
rule {name}
|
||||
command = $cc -MMD -MT $out -MF $out.d {cflags} -c $in -o $out
|
||||
@@ -163,55 +172,67 @@ rule {name}
|
||||
return rule.splitlines()
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class SourceSet:
|
||||
name: str
|
||||
sources: list[str]
|
||||
cflags: list[str]
|
||||
|
||||
def get_objects(self, config):
|
||||
for s in self.sources:
|
||||
yield (config.relbuild(s), re.sub(r"\.\w+", ".o", s))
|
||||
|
||||
def get_targets(self, config):
|
||||
return list(zip(*self.get_objects(config)))[1]
|
||||
|
||||
|
||||
def source_set(name, sources, cflags=()):
|
||||
builds = [
|
||||
(os.path.relpath(s, builddir), re.sub(r"\.\w+", ".o", s))
|
||||
for s in sources
|
||||
if get_suffix_rule(s) is not None
|
||||
]
|
||||
|
||||
lines = []
|
||||
|
||||
cxx_rule = "cxx"
|
||||
cc_rule = "cc"
|
||||
if cflags:
|
||||
cxx_rule = f"cxx_{name}"
|
||||
lines += make_cxx_rule(cxx_rule, cflags=cflags)
|
||||
cc_rule = f"cc_{name}"
|
||||
lines += make_cc_rule(cc_rule, cflags=cflags)
|
||||
|
||||
for i, o in builds:
|
||||
rule = get_suffix_rule(i, cxx_rule=cxx_rule, cc_rule=cc_rule)
|
||||
if rule is None:
|
||||
continue
|
||||
lines.append(f"build {o}: {rule} {i}")
|
||||
|
||||
return [b[1] for b in builds], lines
|
||||
return SourceSet(name, sources, cflags)
|
||||
|
||||
|
||||
def build_source_set(source_set):
|
||||
for line in source_set[1]:
|
||||
yield line
|
||||
def __f(config):
|
||||
cxx_rule = "cxx"
|
||||
cc_rule = "cc"
|
||||
lines = []
|
||||
if source_set.cflags:
|
||||
cxx_rule = f"cxx_{name}"
|
||||
lines += make_cxx_rule(cxx_rule, cflags=cflags + config.cxx_flags)
|
||||
cc_rule = f"cc_{name}"
|
||||
lines += make_cc_rule(cc_rule, cflags=cflags + config.cc_flags)
|
||||
for line in lines:
|
||||
yield line
|
||||
|
||||
for i, o in source_set.get_objects(config):
|
||||
rule = get_suffix_rule(i, cxx_rule=cxx_rule, cc_rule=cc_rule)
|
||||
if rule is None:
|
||||
continue
|
||||
yield f"build {o}: {rule} {i}"
|
||||
|
||||
return __f
|
||||
|
||||
|
||||
def build_image(source_set, elf_out, dependencies=(), bin_out=None, linker_script=linker_script):
|
||||
# to make it builddir-relative
|
||||
linker_script = os.path.relpath(linker_script, builddir)
|
||||
def build_image(source_set, elf_out, linker_script, dependencies=(), bin_out=None):
|
||||
def __f(config):
|
||||
# to make it builddir-relative
|
||||
lscript = config.relbuild(linker_script)
|
||||
elfout = config.relbuild(os.path.join(config.outdir, elf_out))
|
||||
|
||||
elf_out = os.path.relpath(elf_out, builddir)
|
||||
for l in build_source_set(source_set)(config):
|
||||
yield l
|
||||
|
||||
objects, lines = source_set
|
||||
for line in lines:
|
||||
yield line
|
||||
for objs, _ in dependencies:
|
||||
objects += objs
|
||||
objects = " ".join(objects)
|
||||
objects = source_set.get_targets(config)
|
||||
for dep in dependencies:
|
||||
objects += dep.get_targets(config)
|
||||
objects = " ".join(objects)
|
||||
|
||||
yield f"build {elf_out}: link {objects} | {linker_script}"
|
||||
yield f" linker_script = {linker_script}"
|
||||
if bin_out is not None:
|
||||
bin_out = os.path.relpath(bin_out, builddir)
|
||||
yield f"build {bin_out}: objcopy {elf_out}"
|
||||
yield f"build {elfout}: link {objects} | {lscript}"
|
||||
yield f" linker_script = {lscript}"
|
||||
if bin_out is not None:
|
||||
binout = config.relbuild(os.path.join(config.outdir, bin_out))
|
||||
yield f"build {binout}: objcopy {elfout}"
|
||||
|
||||
return __f
|
||||
|
||||
|
||||
def build_test(name, sources):
|
||||
@@ -248,35 +269,48 @@ hal = source_set("hal", [
|
||||
"hal/intc.cc",
|
||||
"hal/interrupts.cc",
|
||||
"hal/start.cc",
|
||||
"hal/lib/common/xil_assert.c",
|
||||
"hal/uart/xuartlite.c",
|
||||
"hal/uart/xuartlite_stats.c",
|
||||
"hal/uart/xuartlite_intr.c",
|
||||
"hal/uart/xuartlite_stats.c",
|
||||
"hal/xilinx/xil_assert.c",
|
||||
])
|
||||
|
||||
lib = source_set("lib", [
|
||||
"lib/async.cc",
|
||||
"lib/lock.cc",
|
||||
])
|
||||
|
||||
bootloader = source_set("bootloader", glob.glob("./bootloader/**/*.cc", recursive=True))
|
||||
bootloader_image = build_image(
|
||||
bootloader,
|
||||
dependencies=[hal],
|
||||
elf_out="out/bootloader.elf",
|
||||
elf_out="bootloader.elf",
|
||||
linker_script="bootloader/bootloader.ld",
|
||||
)
|
||||
|
||||
def app_image(app):
|
||||
def app_image(app, sources=None):
|
||||
if sources is None:
|
||||
sources = glob.glob(f"./apps/{app}/**/*.cc", recursive=True)
|
||||
return build_image(
|
||||
source_set(app, glob.glob(f"./apps/{app}/**/*.cc", recursive=True)),
|
||||
dependencies=[hal],
|
||||
elf_out=f"out/{app}.elf",
|
||||
bin_out=f"out/{app}.bin",
|
||||
source_set(app, sources),
|
||||
linker_script="apps/app.ld",
|
||||
dependencies=[hal, lib],
|
||||
elf_out=f"{app}.elf",
|
||||
bin_out=f"{app}.bin",
|
||||
)
|
||||
|
||||
all = [
|
||||
build_source_set(hal),
|
||||
build_source_set(lib),
|
||||
bootloader_image,
|
||||
|
||||
app_image("helloworld"),
|
||||
app_image("wozmon"),
|
||||
app_image("timer"),
|
||||
app_image("uart"),
|
||||
app_image("async", sources=[
|
||||
"apps/async/main.cc",
|
||||
"apps/async/uart.cc",
|
||||
]),
|
||||
]
|
||||
|
||||
|
||||
@@ -284,24 +318,24 @@ def parse_args():
|
||||
parser = argparse.ArgumentParser(description='Generate ninja build files.')
|
||||
parser.add_argument('--version', required=True,
|
||||
help='version tag (typically from `git describe`)')
|
||||
parser.add_argument('--build-dir', default='build', help='build directory')
|
||||
parser.add_argument('--out-dir', default='out', help='output directory')
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
add_cpp_flag(f'-DGIT_VERSION_TAG=\\"{args.version}\\"')
|
||||
header = gen_rules()
|
||||
lines = itertools.chain(header, *all)
|
||||
config = Config(builddir=args.build_dir, outdir=args.out_dir)
|
||||
config.project_flags.append(f'-DGIT_VERSION_TAG=\\"{args.version}\\"')
|
||||
header = gen_rules(config)
|
||||
lines = itertools.chain(header, *(f(config) for f in all))
|
||||
|
||||
if not os.path.exists(builddir):
|
||||
os.mkdir(builddir)
|
||||
|
||||
with open(os.path.join(builddir, "build.ninja"), "w") as f:
|
||||
with open(os.path.join(config.builddir, "build.ninja"), "w") as f:
|
||||
f.write("\n".join(lines))
|
||||
f.write("\n")
|
||||
|
||||
print(
|
||||
f'Configure done. Build with "ninja -C {builddir}". Output will be in {outdir}/'
|
||||
f'Configure done. Build with "ninja -C {config.builddir}". Output will be in {config.outdir}/'
|
||||
)
|
||||
|
||||
|
||||
|
||||
11
mbv/hal/bios.h
Normal file
11
mbv/hal/bios.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
extern "C" {
|
||||
|
||||
uint8_t BiosUartRead();
|
||||
void BiosUartWrite(uint8_t);
|
||||
void BiosWozmon();
|
||||
void BiosUartWriteNibble(uint8_t);
|
||||
}
|
||||
@@ -7,20 +7,17 @@ void BiosUartWrite(uint8_t);
|
||||
void BiosWozmon();
|
||||
void BiosUartWriteNibble(uint8_t n);
|
||||
|
||||
__attribute__((used))
|
||||
void UartWriteU32(uint32_t a) {
|
||||
__attribute__((used)) void UartWriteU32(uint32_t a) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
BiosUartWriteNibble(a >> 28);
|
||||
a <<= 4;
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((used))
|
||||
void UartWriteString(const char* s) {
|
||||
__attribute__((used)) void UartWriteString(const char* s) {
|
||||
while (*s) {
|
||||
BiosUartWrite(*s);
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
11
mbv/hal/gpio.h
Normal file
11
mbv/hal/gpio.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
struct Gpio {
|
||||
volatile uint32_t data;
|
||||
|
||||
static Gpio* Instance(uint32_t base) {
|
||||
return reinterpret_cast<Gpio*>(base);
|
||||
}
|
||||
};
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "intc.h"
|
||||
|
||||
// this interrupt controller is tied to the pol0 design
|
||||
#include "pol0.h"
|
||||
|
||||
namespace intc {
|
||||
namespace {
|
||||
|
||||
struct IntC {
|
||||
@@ -22,7 +24,7 @@ struct IntC {
|
||||
IntC* intc = reinterpret_cast<IntC*>(INTC_BASE);
|
||||
Isr isrs[NIRQ] = {};
|
||||
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool SetIrqEnabled(uint8_t irqn, bool enabled) {
|
||||
uint32_t mask = 1 << irqn;
|
||||
@@ -37,13 +39,9 @@ bool SetIrqEnabled(uint8_t irqn, bool enabled) {
|
||||
return was_enabled;
|
||||
}
|
||||
|
||||
void SetIsr(uint8_t irqn, Isr isr) {
|
||||
isrs[irqn] = isr;
|
||||
}
|
||||
void SetIsr(uint8_t irqn, Isr isr) { isrs[irqn] = isr; }
|
||||
|
||||
void EnableInterrupts() {
|
||||
intc->MER = 0x3;
|
||||
}
|
||||
void EnableInterrupts() { intc->MER = 0x3; }
|
||||
|
||||
void InterruptHandler() {
|
||||
uint32_t ipr = intc->IPR;
|
||||
@@ -60,3 +58,5 @@ void InterruptHandler() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace intc
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
using Isr = void(*)(void);
|
||||
namespace intc {
|
||||
|
||||
using Isr = void (*)(void);
|
||||
|
||||
/// Returns: true if the IRQ was previously enabled
|
||||
bool SetIrqEnabled(uint8_t irqn, bool enabled);
|
||||
@@ -14,3 +16,5 @@ void EnableInterrupts();
|
||||
|
||||
// Feed this to the CPU's interrupt handler
|
||||
void InterruptHandler();
|
||||
|
||||
} // namespace intc
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "interrupts.h"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "interrupts.h"
|
||||
#include "bios.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -11,8 +13,7 @@ constexpr uint32_t kInterruptCauseMask = 0xff;
|
||||
|
||||
Isr external_handler = nullptr;
|
||||
|
||||
__attribute__((interrupt))
|
||||
void TrapHandler() {
|
||||
__attribute__((interrupt)) void TrapHandler() {
|
||||
uint32_t mcause;
|
||||
uint32_t mip;
|
||||
asm volatile("csrr %0, mcause" : "=r"(mcause));
|
||||
@@ -25,28 +26,39 @@ void TrapHandler() {
|
||||
}
|
||||
|
||||
mip &= ~(kMieExternalInterruptMask);
|
||||
asm volatile("csrw mip, %0" :: "r"(mip));
|
||||
asm volatile("csrw mip, %0" ::"r"(mip));
|
||||
} else {
|
||||
BiosWozmon();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void SetExternalInterruptHandler(Isr handler) {
|
||||
external_handler = handler;
|
||||
}
|
||||
void SetExternalInterruptHandler(Isr handler) { external_handler = handler; }
|
||||
|
||||
void EnableExternalInterrupts() {
|
||||
uint32_t mstatus;
|
||||
uint32_t mie;
|
||||
Isr trap = TrapHandler;
|
||||
|
||||
asm volatile("csrr %0, mstatus" : "=r"(mstatus));
|
||||
asm volatile("csrr %0, mie" : "=r"(mie));
|
||||
|
||||
asm volatile("csrw mtvec, %0" :: "r"(trap));
|
||||
|
||||
asm volatile("csrw mtvec, %0" ::"r"(trap));
|
||||
mie |= kMieExternalInterruptMask;
|
||||
asm volatile("csrw mie, %0" :: "r"(mie));
|
||||
mstatus |= kMstatusMieMask;
|
||||
asm volatile("csrw mstatus, %0" :: "r"(mstatus));
|
||||
asm volatile("csrw mie, %0" ::"r"(mie));
|
||||
}
|
||||
|
||||
bool EnableInterrupts(bool on) {
|
||||
uint32_t mstatus;
|
||||
bool was_on;
|
||||
asm volatile("csrr %0, mstatus" : "=r"(mstatus));
|
||||
|
||||
was_on = (mstatus & kMstatusMieMask) > 0;
|
||||
|
||||
if (on) {
|
||||
mstatus |= kMstatusMieMask;
|
||||
} else {
|
||||
mstatus &= ~kMstatusMieMask;
|
||||
}
|
||||
|
||||
asm volatile("csrw mstatus, %0" ::"r"(mstatus));
|
||||
return was_on;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
using Isr = void(*)();
|
||||
using Isr = void (*)();
|
||||
|
||||
void SetExternalInterruptHandler(Isr handler);
|
||||
void EnableExternalInterrupts();
|
||||
|
||||
/** Returns true if interrupts were enabled, false otherwise */
|
||||
bool EnableInterrupts(bool on);
|
||||
|
||||
@@ -2,15 +2,17 @@
|
||||
|
||||
extern "C" uint32_t _bss_begin, _bss_end, _initial_stack_pointer;
|
||||
extern "C" int main();
|
||||
extern "C" void __libc_init_array();
|
||||
|
||||
__attribute__((section(".start"), used, naked))
|
||||
void _start() {
|
||||
__attribute__((section(".start"), used, naked)) void _start() {
|
||||
// clear .bss
|
||||
for (uint32_t* ptr = &_bss_begin; ptr < &_bss_end; ptr++) {
|
||||
*ptr = 0;
|
||||
}
|
||||
|
||||
asm volatile ( "la sp, _initial_stack_pointer" );
|
||||
asm volatile("la sp, _initial_stack_pointer");
|
||||
|
||||
__libc_init_array();
|
||||
|
||||
main();
|
||||
|
||||
|
||||
@@ -37,11 +37,15 @@ struct Timer {
|
||||
|
||||
void EnableT1() {
|
||||
TCSR1.ARHT0 = 1;
|
||||
TCSR1.ENIT0 = 1; // enable interrupt for overflows
|
||||
TCSR1.ENT0 = 1;
|
||||
}
|
||||
|
||||
uint32_t GetT1Ticks() { return TCR1; }
|
||||
|
||||
bool HasT1Overflowed() { return TCSR1.T0INT; }
|
||||
void ClearT1Overflow() { TCSR1.T0INT = 1; }
|
||||
|
||||
void SetupAsWdt(uint32_t timeout_ticks) {
|
||||
TLR0 = timeout_ticks;
|
||||
TCSR0.LOAD0 = 1; // reset counter
|
||||
@@ -59,9 +63,7 @@ struct Timer {
|
||||
TCSR0.ENT0 = 1;
|
||||
}
|
||||
|
||||
void ClearInterrupt() {
|
||||
TCSR0.T0INT = 0;
|
||||
}
|
||||
void ClearInterrupt() { TCSR0.T0INT = 0; }
|
||||
|
||||
static Timer* Instance(uint32_t base) {
|
||||
return reinterpret_cast<Timer*>(base);
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
DRIVER_LIB_VERSION = 1.0
|
||||
COMPILER=
|
||||
ARCHIVER=
|
||||
CP=cp
|
||||
COMPILER_FLAGS=
|
||||
EXTRA_COMPILER_FLAGS=
|
||||
LIB=libxil.a
|
||||
|
||||
CC_FLAGS = $(COMPILER_FLAGS)
|
||||
ECC_FLAGS = $(EXTRA_COMPILER_FLAGS)
|
||||
|
||||
RELEASEDIR=../../../lib/
|
||||
INCLUDEDIR=../../../include/
|
||||
INCLUDES=-I./. -I$(INCLUDEDIR)
|
||||
|
||||
SRCFILES:=$(wildcard *.c)
|
||||
|
||||
OBJECTS = $(addprefix $(RELEASEDIR), $(addsuffix .o, $(basename $(wildcard *.c))))
|
||||
|
||||
libs: $(OBJECTS)
|
||||
|
||||
DEPFILES := $(SRCFILES:%.c=$(RELEASEDIR)%.d)
|
||||
|
||||
include $(wildcard $(DEPFILES))
|
||||
|
||||
include $(wildcard ../../../../dep.mk)
|
||||
|
||||
$(RELEASEDIR)%.o: %.c
|
||||
${COMPILER} $(CC_FLAGS) $(ECC_FLAGS) $(INCLUDES) $(DEPENDENCY_FLAGS) $< -o $@
|
||||
|
||||
.PHONY: include
|
||||
include: $(addprefix $(INCLUDEDIR),$(wildcard *.h))
|
||||
|
||||
$(INCLUDEDIR)%.h: %.h
|
||||
$(CP) $< $@
|
||||
|
||||
clean:
|
||||
rm -rf ${OBJECTS}
|
||||
rm -rf $(DEPFILES)
|
||||
@@ -387,8 +387,7 @@ int XUartLite_IsSending(XUartLite *InstancePtr)
|
||||
/*
|
||||
* Read the status register to determine if the transmitter is empty
|
||||
*/
|
||||
StatusRegister = XUartLite_ReadReg(InstancePtr->RegBaseAddress,
|
||||
XUL_STATUS_REG_OFFSET);
|
||||
StatusRegister = XUartLite_GetSR(InstancePtr);
|
||||
|
||||
/*
|
||||
* If the transmitter is not empty then indicate that the UART is still
|
||||
|
||||
@@ -143,8 +143,7 @@ void XUartLite_InterruptHandler(XUartLite *InstancePtr)
|
||||
* Read the status register to determine which, coulb be both
|
||||
* interrupt is active
|
||||
*/
|
||||
IsrStatus = XUartLite_ReadReg(InstancePtr->RegBaseAddress,
|
||||
XUL_STATUS_REG_OFFSET);
|
||||
IsrStatus = XUartLite_GetSR(InstancePtr);
|
||||
|
||||
if ((IsrStatus & (XUL_SR_RX_FIFO_FULL |
|
||||
XUL_SR_RX_FIFO_VALID_DATA)) != 0) {
|
||||
|
||||
158
mbv/lib/async.cc
Normal file
158
mbv/lib/async.cc
Normal file
@@ -0,0 +1,158 @@
|
||||
#include "async.h"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <utility>
|
||||
|
||||
#include "lock.h"
|
||||
|
||||
namespace async {
|
||||
namespace {
|
||||
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
struct Stuff {
|
||||
std::coroutine_handle<> h;
|
||||
std::chrono::system_clock::time_point expiration;
|
||||
|
||||
Stuff* next;
|
||||
};
|
||||
|
||||
struct Notification {
|
||||
bool pending; // can only be true if stuff is nullptr
|
||||
Stuff* stuff;
|
||||
};
|
||||
|
||||
std::atomic<Stuff*> work = nullptr;
|
||||
std::array<Notification, static_cast<size_t>(AwaitableType::kNumTypes)>
|
||||
notifications = {};
|
||||
|
||||
} // namespace
|
||||
|
||||
void schedule(std::coroutine_handle<> h, int ms) {
|
||||
InterruptLock lock;
|
||||
std::chrono::system_clock::time_point exp =
|
||||
std::chrono::system_clock::now() + std::chrono::milliseconds(ms);
|
||||
Stuff* news = new Stuff{
|
||||
.h = h,
|
||||
.expiration = exp,
|
||||
};
|
||||
|
||||
Stuff* stuff = work;
|
||||
|
||||
if (!stuff || stuff->expiration > exp) {
|
||||
news->next = stuff;
|
||||
work = news;
|
||||
return;
|
||||
}
|
||||
|
||||
Stuff* s = stuff;
|
||||
while (s->next && s->next->expiration <= exp) {
|
||||
s = s->next;
|
||||
}
|
||||
|
||||
news->next = s->next;
|
||||
s->next = news;
|
||||
}
|
||||
|
||||
void step() {
|
||||
Stuff* stuff;
|
||||
// ensure all previous side effects are visible
|
||||
{
|
||||
InterruptLock lock;
|
||||
stuff = work;
|
||||
};
|
||||
|
||||
if (stuff == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto dt = stuff->expiration - now;
|
||||
|
||||
if (dt > 0ms) {
|
||||
return;
|
||||
}
|
||||
|
||||
int stuffinqueue = 0;
|
||||
for (Stuff* s = stuff; s; s = s->next) stuffinqueue++;
|
||||
|
||||
stuff->h();
|
||||
|
||||
{
|
||||
InterruptLock lock;
|
||||
work = stuff->next;
|
||||
}
|
||||
delete stuff;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
Stuff* stuff = work;
|
||||
while (stuff) {
|
||||
Stuff* byebye = stuff;
|
||||
stuff = stuff->next;
|
||||
|
||||
delete byebye;
|
||||
}
|
||||
work = nullptr;
|
||||
}
|
||||
|
||||
void main_loop(bool (*idle_function)()) {
|
||||
while (1) {
|
||||
if (idle_function != nullptr) {
|
||||
if (idle_function()) {
|
||||
reset();
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
step();
|
||||
}
|
||||
}
|
||||
|
||||
void enqueue(std::coroutine_handle<> h, AwaitableType type) {
|
||||
auto ttype = static_cast<size_t>(type);
|
||||
|
||||
{
|
||||
InterruptLock lock;
|
||||
|
||||
const bool was_notified =
|
||||
std::exchange(notifications[ttype].pending, false);
|
||||
if (was_notified) {
|
||||
schedule(h);
|
||||
return;
|
||||
}
|
||||
|
||||
Stuff* item = new Stuff{.h = h};
|
||||
Stuff* stuff = notifications[ttype].stuff;
|
||||
if (stuff == nullptr) {
|
||||
notifications[ttype].stuff = item;
|
||||
return;
|
||||
}
|
||||
while (stuff->next != nullptr) {
|
||||
stuff = stuff->next;
|
||||
}
|
||||
stuff->next = item;
|
||||
}
|
||||
}
|
||||
|
||||
void resume(AwaitableType type) {
|
||||
auto ttype = static_cast<size_t>(type);
|
||||
Stuff* stuff = nullptr;
|
||||
{
|
||||
InterruptLock lock;
|
||||
|
||||
stuff = notifications[ttype].stuff;
|
||||
if (stuff == nullptr) {
|
||||
notifications[ttype].pending = true;
|
||||
return;
|
||||
}
|
||||
|
||||
notifications[ttype].stuff = stuff->next;
|
||||
schedule(stuff->h);
|
||||
}
|
||||
delete stuff;
|
||||
}
|
||||
|
||||
} // namespace async
|
||||
193
mbv/lib/async.h
Normal file
193
mbv/lib/async.h
Normal file
@@ -0,0 +1,193 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <coroutine>
|
||||
#include <utility>
|
||||
|
||||
namespace async {
|
||||
|
||||
struct continuation : std::suspend_always {
|
||||
void await_suspend(std::coroutine_handle<>) noexcept {
|
||||
if (parent) {
|
||||
parent.resume();
|
||||
}
|
||||
}
|
||||
std::coroutine_handle<> parent;
|
||||
};
|
||||
|
||||
template <typename T = void>
|
||||
struct task;
|
||||
|
||||
template <>
|
||||
struct task<void> {
|
||||
struct promise_type;
|
||||
using handle_type = std::coroutine_handle<promise_type>;
|
||||
|
||||
~task() {
|
||||
if (h) {
|
||||
h.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
struct promise_type {
|
||||
task get_return_object() {
|
||||
return {.h = handle_type::from_promise(*this)};
|
||||
}
|
||||
std::suspend_always initial_suspend() noexcept { return {}; }
|
||||
continuation final_suspend() noexcept { return {.parent = parent}; }
|
||||
void return_void() {}
|
||||
void unhandled_exception() {}
|
||||
|
||||
std::coroutine_handle<> parent;
|
||||
};
|
||||
|
||||
// awaitable
|
||||
bool await_ready() {
|
||||
h.promise().parent = {};
|
||||
h.resume();
|
||||
if (h.done()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void await_suspend(std::coroutine_handle<> ha) { h.promise().parent = ha; }
|
||||
void await_resume() {}
|
||||
|
||||
handle_type h;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct task {
|
||||
struct promise_type;
|
||||
using handle_type = std::coroutine_handle<promise_type>;
|
||||
|
||||
~task() {
|
||||
if (h) {
|
||||
h.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
struct promise_type {
|
||||
task get_return_object() {
|
||||
return {.h = handle_type::from_promise(*this)};
|
||||
}
|
||||
std::suspend_always initial_suspend() noexcept { return {}; }
|
||||
continuation final_suspend() noexcept { return {.parent = parent}; }
|
||||
void return_value(T&& value) { ret_value = std::move(value); }
|
||||
void unhandled_exception() {}
|
||||
template <std::convertible_to<T> From>
|
||||
continuation yield_value(From&& value) {
|
||||
ret_value = std::forward<From>(value);
|
||||
result_ready = true;
|
||||
return {.parent = parent};
|
||||
}
|
||||
|
||||
T ret_value;
|
||||
bool result_ready = false;
|
||||
std::coroutine_handle<> parent;
|
||||
};
|
||||
|
||||
// awaitable
|
||||
bool await_ready() {
|
||||
h.promise().parent = {};
|
||||
h.resume();
|
||||
if (h.promise().result_ready || h.done()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void await_suspend(std::coroutine_handle<> ha) { h.promise().parent = ha; }
|
||||
T await_resume() {
|
||||
h.promise().result_ready = false;
|
||||
return std::move(h.promise().ret_value);
|
||||
}
|
||||
|
||||
std::coroutine_handle<promise_type> h;
|
||||
};
|
||||
|
||||
enum class AwaitableType {
|
||||
kUnknown = 0,
|
||||
kUartRx = 1,
|
||||
kUartTx = 2,
|
||||
|
||||
kNumTypes
|
||||
};
|
||||
|
||||
void schedule(std::coroutine_handle<> h, int ms = 0);
|
||||
void enqueue(std::coroutine_handle<> h, AwaitableType type);
|
||||
void resume(AwaitableType type); // typically called from an ISR
|
||||
|
||||
void main_loop(bool (*idle_function)());
|
||||
void step();
|
||||
|
||||
inline auto await(AwaitableType type) {
|
||||
struct awaitable {
|
||||
AwaitableType type;
|
||||
|
||||
bool await_ready() { return false; };
|
||||
void await_suspend(std::coroutine_handle<> h) { enqueue(h, type); }
|
||||
void await_resume() {}
|
||||
};
|
||||
|
||||
return awaitable{type};
|
||||
}
|
||||
|
||||
inline auto delay(int ms) {
|
||||
struct awaitable {
|
||||
int ms;
|
||||
|
||||
bool await_ready() { return false; };
|
||||
void await_suspend(std::coroutine_handle<> h) { schedule(h, ms); }
|
||||
void await_resume() {}
|
||||
};
|
||||
|
||||
return awaitable{ms};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct gimme {
|
||||
// child interface
|
||||
bool await_ready() { return false; }
|
||||
void await_suspend(std::coroutine_handle<> h) {
|
||||
ha = h;
|
||||
waiting = true;
|
||||
if (parent) {
|
||||
schedule(parent);
|
||||
}
|
||||
}
|
||||
T await_resume() {
|
||||
waiting = false;
|
||||
return std::move(stuff);
|
||||
}
|
||||
|
||||
// parent interface
|
||||
auto feed(T&& s) {
|
||||
struct awaitable {
|
||||
bool await_ready() {
|
||||
g.parent = {};
|
||||
g.ha.resume();
|
||||
return g.waiting;
|
||||
}
|
||||
void await_suspend(std::coroutine_handle<> h) { g.parent = h; }
|
||||
void await_resume() {}
|
||||
|
||||
gimme<T>& g;
|
||||
};
|
||||
|
||||
if (!waiting) {
|
||||
__builtin_trap();
|
||||
}
|
||||
if (!ha) {
|
||||
__builtin_trap();
|
||||
}
|
||||
stuff = std::move(s);
|
||||
return awaitable{.g = *this};
|
||||
}
|
||||
|
||||
bool waiting = false;
|
||||
std::coroutine_handle<> ha;
|
||||
std::coroutine_handle<> parent;
|
||||
T stuff;
|
||||
};
|
||||
|
||||
} // namespace async
|
||||
30
mbv/lib/buffer.h
Normal file
30
mbv/lib/buffer.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
#include <utility>
|
||||
|
||||
struct buffer {
|
||||
std::span<std::byte> data;
|
||||
|
||||
buffer() = default;
|
||||
buffer(std::span<std::byte> d) : data(d) {}
|
||||
|
||||
static buffer make(size_t size) {
|
||||
return buffer({new std::byte[size], size});
|
||||
}
|
||||
|
||||
buffer(buffer& other) = delete;
|
||||
buffer& operator=(buffer& other) = delete;
|
||||
|
||||
buffer(buffer&& other) : data(std::exchange(other.data, {})) {}
|
||||
buffer& operator=(buffer&& other) {
|
||||
data = std::exchange(other.data, {});
|
||||
return *this;
|
||||
}
|
||||
|
||||
~buffer() {
|
||||
if (data.data()) {
|
||||
delete[] data.data();
|
||||
};
|
||||
}
|
||||
};
|
||||
13
mbv/lib/itoa.h
Normal file
13
mbv/lib/itoa.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
// out must be at least 8 bytes long
|
||||
inline void itoa(int val, char* out) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
uint8_t digit = (val >> (28 - 4 * i)) & 0xf;
|
||||
if (digit < 0xa) {
|
||||
out[i] = '0' + digit;
|
||||
} else {
|
||||
out[i] = 'a' + digit - 0xa;
|
||||
}
|
||||
}
|
||||
}
|
||||
5
mbv/lib/lock.cc
Normal file
5
mbv/lib/lock.cc
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "lock.h"
|
||||
|
||||
#ifdef __x86_64__
|
||||
std::recursive_mutex InterruptLock::m;
|
||||
#endif
|
||||
21
mbv/lib/lock.h
Normal file
21
mbv/lib/lock.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef __x86_64__
|
||||
#include "hal/interrupts.h"
|
||||
|
||||
struct InterruptLock {
|
||||
bool was_on;
|
||||
|
||||
InterruptLock() : was_on(EnableInterrupts(false)) {}
|
||||
|
||||
~InterruptLock() { EnableInterrupts(was_on); }
|
||||
};
|
||||
#else // __x86_64__
|
||||
#include <mutex>
|
||||
|
||||
struct InterruptLock {
|
||||
static std::recursive_mutex m;
|
||||
InterruptLock() { m.lock(); }
|
||||
~InterruptLock() { m.unlock(); }
|
||||
};
|
||||
#endif // __x86_64__
|
||||
105
mbv/lib/ring_buffer.h
Normal file
105
mbv/lib/ring_buffer.h
Normal file
@@ -0,0 +1,105 @@
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
|
||||
#include "lock.h"
|
||||
|
||||
struct RingBuffer {
|
||||
std::span<std::byte> buffer;
|
||||
|
||||
size_t read_ptr = 0;
|
||||
size_t write_ptr = 0;
|
||||
size_t used = 0;
|
||||
|
||||
bool Store(std::span<const std::byte> data) {
|
||||
InterruptLock lock;
|
||||
|
||||
if (data.size() > FreeSpace()) {
|
||||
return false;
|
||||
}
|
||||
const size_t to_copy = std::min(buffer.size() - write_ptr, data.size());
|
||||
std::copy(data.begin(), data.begin() + to_copy,
|
||||
buffer.begin() + write_ptr);
|
||||
if (to_copy < data.size()) {
|
||||
std::copy(data.begin() + to_copy, data.end(), buffer.begin());
|
||||
}
|
||||
Push(data.size());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Load(std::span<std::byte> out) {
|
||||
InterruptLock lock;
|
||||
|
||||
if (out.size() > AvailableData()) {
|
||||
return false;
|
||||
}
|
||||
const size_t to_copy = std::min(buffer.size() - read_ptr, out.size());
|
||||
std::copy(buffer.begin() + read_ptr,
|
||||
buffer.begin() + read_ptr + to_copy, out.begin());
|
||||
if (to_copy < out.size()) {
|
||||
std::copy(buffer.begin(), buffer.begin() + out.size() - to_copy,
|
||||
out.begin() + to_copy);
|
||||
}
|
||||
Pop(out.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Push(size_t amount) {
|
||||
InterruptLock lock;
|
||||
|
||||
if (amount > FreeSpace()) {
|
||||
return false;
|
||||
}
|
||||
write_ptr = (write_ptr + amount) % buffer.size();
|
||||
used = used + amount;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Pop(size_t amount) {
|
||||
InterruptLock lock;
|
||||
|
||||
if (amount > AvailableData()) {
|
||||
return false;
|
||||
}
|
||||
read_ptr = (read_ptr + amount) % buffer.size();
|
||||
used = used - amount;
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t FreeSpace() const {
|
||||
InterruptLock lock;
|
||||
|
||||
return buffer.size() - used;
|
||||
}
|
||||
|
||||
size_t AvailableData() const {
|
||||
InterruptLock lock;
|
||||
|
||||
return used;
|
||||
}
|
||||
|
||||
uint8_t* RawReadPointer() const {
|
||||
InterruptLock lock;
|
||||
|
||||
return reinterpret_cast<uint8_t*>(buffer.data() + read_ptr);
|
||||
}
|
||||
|
||||
uint8_t* RawWritePointer() const {
|
||||
InterruptLock lock;
|
||||
|
||||
return reinterpret_cast<uint8_t*>(buffer.data() + write_ptr);
|
||||
}
|
||||
|
||||
size_t ContiguousFreeSpace() const {
|
||||
InterruptLock lock;
|
||||
|
||||
return std::min(FreeSpace(), buffer.size() - write_ptr);
|
||||
}
|
||||
|
||||
size_t ContiguousAvailableData() const {
|
||||
InterruptLock lock;
|
||||
|
||||
return std::min(AvailableData(), buffer.size() - read_ptr);
|
||||
}
|
||||
};
|
||||
38
mbv/prog.py
38
mbv/prog.py
@@ -6,33 +6,33 @@ import threading
|
||||
import time
|
||||
|
||||
offset = 0x80000000
|
||||
tty = '/dev/ttyUSB1'
|
||||
tty = "/dev/ttyUSB1"
|
||||
baud = 115200
|
||||
chunksize = 128
|
||||
chunksize = 1024
|
||||
|
||||
|
||||
def write(s, offset, dat):
|
||||
for i in range(0, len(dat), chunksize):
|
||||
chunk = dat[i: i + chunksize]
|
||||
cmd = struct.pack('<cII', b'c', offset + i, len(chunk))
|
||||
chunk = dat[i : i + chunksize]
|
||||
cmd = struct.pack("<cII", b"c", offset + i, len(chunk))
|
||||
|
||||
print(f'Sending {len(chunk)} bytes @0x{offset + i:04x}')
|
||||
print(f"Sending {len(chunk)} bytes @0x{offset + i:04x}")
|
||||
|
||||
s.write(cmd)
|
||||
s.write(chunk)
|
||||
|
||||
ack = s.read(2)
|
||||
if len(ack) < 2:
|
||||
raise RuntimeError(f'timeout waiting for full ack. got {ack}')
|
||||
if ack[0] != b'a'[0]:
|
||||
raise RuntimeError(f'expected ack, got this instead: {ack}')
|
||||
print(f'Ack! len={ack[1]}')
|
||||
raise RuntimeError(f"timeout waiting for full ack. got {ack}")
|
||||
if ack[0] != b"a"[0]:
|
||||
raise RuntimeError(f"expected ack, got this instead: {ack}")
|
||||
print(f"Ack! len={ack[1]}")
|
||||
|
||||
|
||||
def jump(s, offset):
|
||||
cmd = struct.pack('<cI', b'j', offset)
|
||||
print(f'Jumping to 0x{offset:04x}')
|
||||
s.write(cmd)
|
||||
cmd = struct.pack("<cI", b"j", offset)
|
||||
print(f"Jumping to 0x{offset:04x}")
|
||||
s.write(cmd)
|
||||
|
||||
|
||||
def stream_logs(s):
|
||||
@@ -40,21 +40,25 @@ def stream_logs(s):
|
||||
dat = s.read()
|
||||
if not dat:
|
||||
continue
|
||||
sys.stdout.buffer.write(dat.replace(b'\r', b''))
|
||||
sys.stdout.buffer.write(dat.replace(b"\r", b""))
|
||||
sys.stdout.buffer.flush()
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("binary")
|
||||
parser.add_argument("--monitor", action="store_true",
|
||||
help="wait for and display program serial output")
|
||||
parser.add_argument(
|
||||
"--monitor",
|
||||
action="store_true",
|
||||
help="wait for and display program serial output",
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
|
||||
with open(args.binary, 'rb') as f:
|
||||
with open(args.binary, "rb") as f:
|
||||
dat = f.read()
|
||||
|
||||
s = serial.Serial(tty, baud, timeout=1)
|
||||
@@ -67,7 +71,7 @@ def main():
|
||||
|
||||
try:
|
||||
while True:
|
||||
dat = input("") + '\r'
|
||||
dat = input("") + "\r"
|
||||
s.write(dat.encode())
|
||||
except KeyboardInterrupt:
|
||||
print("Bye.")
|
||||
|
||||
62
mbv/testecho.py
Normal file
62
mbv/testecho.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import argparse
|
||||
import random
|
||||
import serial
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
tty = "/dev/ttyUSB1"
|
||||
baud = 115200
|
||||
|
||||
|
||||
def sendrecv(s, size, timeout=5):
|
||||
"""Send some data and recv until the same data was received back."""
|
||||
data = random.randbytes(size)
|
||||
return sendcheck(s, data, timeout)
|
||||
|
||||
|
||||
def sendcheck(s, data, timeout=5):
|
||||
s.write(data)
|
||||
received = 0
|
||||
start = time.time()
|
||||
got = bytearray()
|
||||
while received < len(data):
|
||||
r = s.read()
|
||||
got += r
|
||||
try:
|
||||
assert(r == data[received: received + len(r)])
|
||||
assert(time.time() < start + timeout)
|
||||
except:
|
||||
fr = received - 10
|
||||
to = received + len(r) + 1
|
||||
sdat = data[fr: to]
|
||||
rdat = bytes(got[fr: to])
|
||||
print(f"failed after receiving {received} correct bytes")
|
||||
print(f"expected {sdat}")
|
||||
print(f" got {rdat}")
|
||||
sys.exit(-1)
|
||||
|
||||
received += len(r)
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser()
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
|
||||
s = serial.Serial(tty, baud, timeout=1)
|
||||
for i in range(12):
|
||||
size = 2**i
|
||||
print(f"Trying with {size} bytes")
|
||||
sendrecv(s, size)
|
||||
data = b'ze%s\x96:M#\xd8\x98\x9d\x96\xf5?\x80c\xc6\xa7\x03\xe0i\x04V\xcb\xa3\x95#GC\xabf\x98'
|
||||
#sendcheck(s, data)
|
||||
|
||||
print("All sizes passed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1490,7 +1490,7 @@ def parse_tree(tree, debug=False):
|
||||
|
||||
def larkparse(f, debug=False):
|
||||
with open(GRAMMAR_FILE) as g:
|
||||
asparser = lark.Lark(g.read())
|
||||
asparser = lark.Lark(g.read(), maybe_placeholders=False)
|
||||
data = f.read()
|
||||
if isinstance(data, bytes):
|
||||
data = data.decode()
|
||||
|
||||
@@ -69,7 +69,7 @@ def dump(secmap):
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description='Assemble.')
|
||||
parser = argparse.ArgumentParser(description='Link.')
|
||||
parser.add_argument('--debug', action='store_true',
|
||||
help='print debug info')
|
||||
parser.add_argument('objfiles', metavar='O', nargs='+',
|
||||
|
||||
17
wave/Makefile
Normal file
17
wave/Makefile
Normal file
@@ -0,0 +1,17 @@
|
||||
all: sim
|
||||
sim: pdmout_test.ghw square_test.ghw wave_test.ghw
|
||||
|
||||
sim_sources = pdmout_test.vhdl square_test.vhdl wave_test.vhdl
|
||||
sources = pdmout.vhdl square.vhdl wave.vhdl pdm.vhdl
|
||||
|
||||
|
||||
%.ghw: work-obj93.cf
|
||||
ghdl -r $* --wave=$@
|
||||
|
||||
|
||||
work-obj93.cf: $(sim_sources) $(sources)
|
||||
ghdl -a $^
|
||||
|
||||
PHONY: sim
|
||||
|
||||
.PRECIOUS: test.ghw
|
||||
@@ -1,17 +0,0 @@
|
||||
all: sim
|
||||
sim: pdmout_test.ghw square_test.ghw
|
||||
|
||||
sim_sources = pdmout_test.vhdl square_test.vhdl
|
||||
sources = pdmout.vhdl square.vhdl
|
||||
|
||||
|
||||
%.ghw: work-obj93.cf
|
||||
ghdl -r $* --wave=$@
|
||||
|
||||
|
||||
work-obj93.cf: $(sim_sources) $(sources)
|
||||
ghdl -a $^
|
||||
|
||||
PHONY: sim
|
||||
|
||||
.PRECIOUS: test.ghw
|
||||
46
wave/pdm.vhdl
Normal file
46
wave/pdm.vhdl
Normal file
@@ -0,0 +1,46 @@
|
||||
library ieee;
|
||||
use ieee.std_logic_1164.all;
|
||||
use ieee.numeric_std.all;
|
||||
|
||||
entity pdm is
|
||||
port
|
||||
(
|
||||
clk : in std_logic;
|
||||
rst : in std_logic;
|
||||
|
||||
-- hardware
|
||||
out_pin : out std_logic;
|
||||
|
||||
-- input interface
|
||||
enabled : in std_logic;
|
||||
sample : in std_logic_vector(15 downto 0)
|
||||
);
|
||||
end pdm;
|
||||
|
||||
architecture Behavioral of pdm is
|
||||
|
||||
signal feedback: signed(16 downto 0);
|
||||
|
||||
begin
|
||||
|
||||
-- PDM process
|
||||
-- drives pin_out, feedback
|
||||
process(clk, rst)
|
||||
begin
|
||||
|
||||
if rst = '1' then
|
||||
feedback <= to_signed(0, 17);
|
||||
out_pin <= '0';
|
||||
elsif rising_edge(clk) and enabled = '1' then
|
||||
if feedback > 0 then
|
||||
out_pin <= '1';
|
||||
feedback <= feedback + signed("0" & sample) - ("0" & x"ffff");
|
||||
else
|
||||
out_pin <= '0';
|
||||
feedback <= feedback + signed("0" & sample);
|
||||
end if;
|
||||
end if;
|
||||
|
||||
end process;
|
||||
|
||||
end Behavioral;
|
||||
175
wave/wave.vhdl
Normal file
175
wave/wave.vhdl
Normal file
@@ -0,0 +1,175 @@
|
||||
library ieee;
|
||||
use ieee.std_logic_1164.all;
|
||||
use ieee.numeric_std.all;
|
||||
|
||||
-- some kind of square wave generator, with PDM output
|
||||
entity wave is
|
||||
port
|
||||
(
|
||||
-- AXI4 slave interface
|
||||
aclk : in std_logic;
|
||||
aresetn : in std_logic;
|
||||
|
||||
-- read addr
|
||||
araddr : in std_logic_vector(31 downto 0);
|
||||
arvalid : in std_logic;
|
||||
arready : out std_logic;
|
||||
|
||||
-- read data
|
||||
rdata : out std_logic_vector(31 downto 0);
|
||||
rresp : out std_logic;
|
||||
rvalid : out std_logic;
|
||||
rready : in std_logic;
|
||||
|
||||
-- write addr
|
||||
awaddr : in std_logic_vector(31 downto 0);
|
||||
awvalid : in std_logic;
|
||||
awready : out std_logic;
|
||||
|
||||
-- write data
|
||||
wdata : in std_logic_vector(31 downto 0);
|
||||
wstrb : in std_logic_vector(3 downto 0);
|
||||
|
||||
-- write resp
|
||||
bresp : out std_logic;
|
||||
bvalid : out std_logic;
|
||||
bready : in std_logic;
|
||||
|
||||
|
||||
-- PDM output!
|
||||
pdmout : out std_logic
|
||||
);
|
||||
end wave;
|
||||
|
||||
--
|
||||
-- Mem layout:
|
||||
-- 0x00: control: [bit0: enabled]
|
||||
-- 0x04: 32-bit unsigned period (in clock cycles)
|
||||
-- 0x08: 16-bit unsigned high value (high 16 bits ignored)
|
||||
-- 0x0c: 16-bit unsigned low value (high 16 bits ignored)
|
||||
|
||||
architecture Behavioral of wave is
|
||||
|
||||
component pdm is
|
||||
port
|
||||
(
|
||||
clk : in std_logic;
|
||||
rst : in std_logic;
|
||||
|
||||
-- hardware
|
||||
out_pin : out std_logic;
|
||||
|
||||
-- input interface
|
||||
enabled : in std_logic;
|
||||
sample : in std_logic_vector(15 downto 0)
|
||||
);
|
||||
end component;
|
||||
|
||||
-- all registers
|
||||
signal period, hi_val, lo_val, sample: std_logic_vector(15 downto 0);
|
||||
signal enabled, rst, onwrite: std_logic;
|
||||
signal counter: unsigned(23 downto 0);
|
||||
|
||||
begin
|
||||
|
||||
arready <= '1';
|
||||
awready <= '1';
|
||||
rst <= not aresetn;
|
||||
|
||||
pdm0: pdm port map(clk => aclk, rst => rst, out_pin => pdmout,
|
||||
sample => sample, enabled => enabled);
|
||||
|
||||
-- main process
|
||||
-- drives counter, sample
|
||||
process(aclk, aresetn)
|
||||
variable high: std_logic;
|
||||
begin
|
||||
|
||||
if aresetn = '0' then
|
||||
counter <= to_unsigned(0, 24);
|
||||
sample <= x"0000";
|
||||
high := '0';
|
||||
elsif rising_edge(aclk) then
|
||||
counter <= to_unsigned(0, 24);
|
||||
if high = '1' then
|
||||
sample <= hi_val;
|
||||
else
|
||||
sample <= lo_val;
|
||||
end if;
|
||||
|
||||
if enabled = '1' and unsigned(period) /= 0 and onwrite = '0' then
|
||||
if counter(22 downto 7) = unsigned(period) - 1
|
||||
and counter(6 downto 0) = x"7f" then
|
||||
high := not high;
|
||||
else
|
||||
counter <= counter + 1;
|
||||
end if;
|
||||
end if;
|
||||
end if;
|
||||
|
||||
end process;
|
||||
|
||||
-- Bus slave process
|
||||
-- drives period, value, out_addr, enabled, onwrite
|
||||
process(aclk, aresetn)
|
||||
-- 00: control, 01: period, 02: high, 03: low
|
||||
variable waddr: std_logic_vector(2 downto 0);
|
||||
variable raddr: std_logic_vector(2 downto 0);
|
||||
begin
|
||||
|
||||
if aresetn = '0' then
|
||||
period <= x"0000";
|
||||
hi_val <= x"0000";
|
||||
lo_val <= x"0000";
|
||||
enabled <= '0';
|
||||
waddr := "000";
|
||||
|
||||
elsif rising_edge(aclk) then
|
||||
|
||||
rvalid <= '0';
|
||||
rdata <= x"00000000";
|
||||
rresp <= '0';
|
||||
bvalid <= '0';
|
||||
bresp <= '0';
|
||||
onwrite <= '0';
|
||||
|
||||
if arvalid = '1' then
|
||||
raddr := araddr(4 downto 2);
|
||||
end if;
|
||||
if awvalid = '1' then
|
||||
waddr := awaddr(4 downto 2);
|
||||
end if;
|
||||
if arvalid = '1' then
|
||||
case waddr(1 downto 0) is
|
||||
when "00" =>
|
||||
rdata(0) <= enabled;
|
||||
when "01" =>
|
||||
rdata(23 downto 8) <= period;
|
||||
when "10" =>
|
||||
rdata(15 downto 0) <= hi_val;
|
||||
when "11" =>
|
||||
rdata(15 downto 0) <= lo_val;
|
||||
when others =>
|
||||
end case;
|
||||
rvalid <= '1';
|
||||
end if;
|
||||
if unsigned(wstrb) > 0 then
|
||||
case waddr(1 downto 0) is
|
||||
when "00" =>
|
||||
enabled <= wdata(0);
|
||||
when "01" =>
|
||||
period <= wdata(23 downto 8);
|
||||
when "10" =>
|
||||
hi_val <= wdata(15 downto 0);
|
||||
when "11" =>
|
||||
lo_val <= wdata(15 downto 0);
|
||||
when others =>
|
||||
end case;
|
||||
bvalid <= '1';
|
||||
onwrite <= '1';
|
||||
end if;
|
||||
end if;
|
||||
|
||||
end process;
|
||||
|
||||
end Behavioral;
|
||||
165
wave/wave_test.vhdl
Normal file
165
wave/wave_test.vhdl
Normal file
@@ -0,0 +1,165 @@
|
||||
library ieee;
|
||||
use ieee.std_logic_1164.all;
|
||||
use ieee.numeric_std.all;
|
||||
use std.textio.all;
|
||||
|
||||
entity wave_test is
|
||||
end wave_test;
|
||||
|
||||
architecture rtl of wave_test is
|
||||
|
||||
component wave is
|
||||
port
|
||||
(
|
||||
-- AXI4 slave interface
|
||||
aclk : in std_logic;
|
||||
aresetn : in std_logic;
|
||||
|
||||
-- read addr
|
||||
araddr : in std_logic_vector(31 downto 0);
|
||||
arvalid : in std_logic;
|
||||
arready : out std_logic;
|
||||
|
||||
-- read data
|
||||
rdata : out std_logic_vector(31 downto 0);
|
||||
rresp : out std_logic;
|
||||
rvalid : out std_logic;
|
||||
rready : in std_logic;
|
||||
|
||||
-- write addr
|
||||
awaddr : in std_logic_vector(31 downto 0);
|
||||
awvalid : in std_logic;
|
||||
awready : out std_logic;
|
||||
|
||||
-- write data
|
||||
wdata : in std_logic_vector(31 downto 0);
|
||||
wstrb : in std_logic_vector(3 downto 0);
|
||||
|
||||
-- write resp
|
||||
bresp : out std_logic;
|
||||
bvalid : out std_logic;
|
||||
bready : in std_logic;
|
||||
|
||||
|
||||
-- PDM output!
|
||||
pdmout : out std_logic
|
||||
);
|
||||
end component;
|
||||
|
||||
signal finished, clk, rst, rstn: std_logic := '0';
|
||||
|
||||
signal arvalid, arready, awvalid, awready: std_logic;
|
||||
signal rresp, rvalid, rready, bresp, bvalid, bready: std_logic;
|
||||
signal wstrb: std_logic_vector(3 downto 0);
|
||||
signal araddr, rdata, awaddr, wdata: std_logic_vector(31 downto 0);
|
||||
|
||||
signal pdmout: std_logic;
|
||||
|
||||
constant period: integer := 7 * 256;
|
||||
constant half_time: time := 1280 ns * period;
|
||||
|
||||
begin
|
||||
rstn <= not rst;
|
||||
dut: wave port map(aclk => clk, aresetn => rstn,
|
||||
arvalid => arvalid, arready => arready, awvalid => awvalid, awready => awready,
|
||||
rresp => rresp, rvalid => rvalid, rready => rready, bresp => bresp, bvalid => bvalid, bready => bready,
|
||||
wstrb => wstrb, araddr => araddr, rdata => rdata, awaddr => awaddr, wdata => wdata,
|
||||
pdmout => pdmout
|
||||
);
|
||||
|
||||
-- tick tock
|
||||
process
|
||||
begin
|
||||
if finished = '0' then
|
||||
clk <= not clk;
|
||||
wait for 5 ns;
|
||||
else
|
||||
clk <= '0';
|
||||
wait;
|
||||
end if;
|
||||
end process;
|
||||
|
||||
process
|
||||
begin
|
||||
rst <= '1';
|
||||
|
||||
arvalid <= '0';
|
||||
awvalid <= '0';
|
||||
wstrb <= "0000";
|
||||
|
||||
wait for 1 ns;
|
||||
-- assert(='0') report "Fail rst" severity error;
|
||||
|
||||
rst <= '0';
|
||||
|
||||
wait for 10 ns;
|
||||
|
||||
awvalid <= '1';
|
||||
awaddr <= x"00000000"; -- enable
|
||||
wstrb <= "1111";
|
||||
wdata <= x"00000001";
|
||||
bready <= '1';
|
||||
|
||||
wait for 10 ns;
|
||||
assert(bvalid = '1') report "Write error" severity error;
|
||||
assert(bresp = '0') report "Write fail" severity error;
|
||||
assert(awready = '1') report "Write error" severity error;
|
||||
|
||||
awaddr <= x"00000004"; -- period
|
||||
wdata <= std_logic_vector(to_unsigned(period, 32));
|
||||
|
||||
wait for 10 ns;
|
||||
|
||||
-- read back
|
||||
wstrb <= "0000";
|
||||
awvalid <= '0';
|
||||
arvalid <= '1';
|
||||
araddr <= x"00000004";
|
||||
rready <= '1';
|
||||
|
||||
wait for 10 ns;
|
||||
assert(unsigned(rdata) = period) report "Period readback failed" severity error;
|
||||
|
||||
arvalid <= '0';
|
||||
awvalid <= '1';
|
||||
wstrb <= "1111";
|
||||
|
||||
awaddr <= x"00000008"; -- high
|
||||
wdata <= x"00008000";
|
||||
|
||||
wait for 10 ns;
|
||||
|
||||
awaddr <= x"0000000c"; -- low
|
||||
wdata <= x"00004000";
|
||||
|
||||
wait for 10 ns;
|
||||
|
||||
wstrb <= "0000";
|
||||
awvalid <= '0';
|
||||
|
||||
wait for 30 ns;
|
||||
assert(pdmout = '1') report "wrong pdmout" severity error;
|
||||
wait for 10 ns;
|
||||
assert(pdmout = '0') report "wrong pdmout" severity error;
|
||||
wait for 10 ns;
|
||||
assert(pdmout = '0') report "wrong pdmout" severity error;
|
||||
wait for 10 ns;
|
||||
assert(pdmout = '1') report "wrong pdmout" severity error;
|
||||
wait for 10 ns;
|
||||
assert(pdmout = '0') report "wrong pdmout" severity error;
|
||||
wait for 10 ns;
|
||||
assert(pdmout = '0') report "wrong pdmout" severity error;
|
||||
wait for 10 ns;
|
||||
assert(pdmout = '0') report "wrong pdmout" severity error;
|
||||
wait for 10 ns;
|
||||
assert(pdmout = '1') report "wrong pdmout" severity error;
|
||||
|
||||
wait for 100 us;
|
||||
|
||||
assert false report "Test done." severity note;
|
||||
|
||||
finished <= '1';
|
||||
wait;
|
||||
|
||||
end process;
|
||||
end rtl;
|
||||
Reference in New Issue
Block a user