aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlavian Kaufmann <flavian@flaviankaufmann.ch>2024-05-20 16:48:55 +0200
committerFlavian Kaufmann <flavian@flaviankaufmann.ch>2024-05-20 16:48:55 +0200
commite945c80a6ceaef501350de49bf647ae8539d1cbb (patch)
tree9e698d839e5f6221af523b49be04b809648efeb9
parent142510b8325b9ef89bd3e22463f36c3caa2815de (diff)
downloadriscv_cpu-e945c80a6ceaef501350de49bf647ae8539d1cbb.tar.gz
riscv_cpu-e945c80a6ceaef501350de49bf647ae8539d1cbb.zip
reset synchronizer
-rw-r--r--Makefile6
-rw-r--r--include/consts.vh12
-rw-r--r--prog/link.ld120
-rw-r--r--prog/src/main.c11
-rw-r--r--prog/src/prog.s.tst (renamed from prog/src/prog.s)0
-rw-r--r--prog/src/startup.c39
-rw-r--r--prog/src/startup.s59
-rw-r--r--src/pc_reg.v2
-rw-r--r--src/reset_synchronizer.v22
-rw-r--r--src/top.v18
10 files changed, 152 insertions, 137 deletions
diff --git a/Makefile b/Makefile
index b364bde..e5a37b7 100644
--- a/Makefile
+++ b/Makefile
@@ -45,6 +45,7 @@ RISCV_AS = $(RISCV_TOOLCHAIN)-as
RISCV_CC = $(RISCV_TOOLCHAIN)-gcc
RISCV_LD = $(RISCV_TOOLCHAIN)-ld
RISCV_OBJCOPY = $(RISCV_TOOLCHAIN)-objcopy
+RISCV_OBJDUMP = $(RISCV_TOOLCHAIN)-objdump
RISCV_ASFLAGS = -march=rv32i -mabi=ilp32
RISCV_CFLAGS = -march=rv32i -mabi=ilp32
@@ -159,9 +160,12 @@ $(BUILD_DIR):
wave:
$(GTKWAVE) -a debug/cpu.gtkw $(BUILD_DIR)/waveform_cpu.vcd
+objdump: $(PROG_ELF_FILE)
+ $(RISCV_OBJDUMP) -d -x --disassembler-color=on $(PROG_ELF_FILE)
+
# Clean
clean:
rm -rf $(BUILD_DIR)
-.PHONY: all simulate rom bitsream upload flash wave clean
+.PHONY: all simulate rom bitsream upload flash wave objdump clean
diff --git a/include/consts.vh b/include/consts.vh
index be1c647..e0f6e5f 100644
--- a/include/consts.vh
+++ b/include/consts.vh
@@ -126,14 +126,4 @@ parameter ROM_END = 32'h000F_FFFF;
parameter RAM_BEGIN = 32'h0010_0000;
parameter RAM_END = 32'hFF0F_FFFF;
-// 0000 0000 Reserved
-// 0000 FFFF
-//
-// 0001 0000 ROM
-// 000F FFFF
-//
-// 0010 0000 RAM
-// FF0F FFFF
-//
-// FF10 0000 Reserved
-// FFFF FFFF
+parameter PC_INITIAL = 32'h0001_0000;
diff --git a/prog/link.ld b/prog/link.ld
index 9644d59..ae0988f 100644
--- a/prog/link.ld
+++ b/prog/link.ld
@@ -1,67 +1,79 @@
-/* Linker script for RISC-V */
-
-/* Define memory regions */
MEMORY
{
- ROM (rx) : ORIGIN = 0x00010000, LENGTH = 0x1000 /* 1024 bytes for ROM */
- RAM (rwx) : ORIGIN = 0x00100000, LENGTH = 0x1000 /* 1024 bytes for RAM */
+ ROM (rx) : ORIGIN = 0x00010000, LENGTH = 4K
+ RAM (rwx) : ORIGIN = 0x00100000, LENGTH = 4K
}
ENTRY(_start)
-/* Define sections */
SECTIONS
{
- /* Code section */
- .text :
- {
- KEEP(*(.init))
- KEEP(*(.text))
- KEEP(*(.fini))
- KEEP(*(.rodata))
- _etext = .;
- } > ROM
+ .text :
+ {
+ _stext = .;
+ . = ORIGIN(ROM);
+ _start = .; /* Entry point address */
+ KEEP(*(.text.startup)) /* Ensure startup code is kept */
+ *(.text) /* .text sections (code) */
+ *(.text.*)
+ . = ALIGN(4);
+ _etext = .; /* End of code section, needed for copying .data */
+ } > ROM
- /* Initialized data section */
- .data : AT(_etext)
- {
- _sdata = .;
- KEEP(*(.data))
- _edata = .;
- } > RAM
+ .rodata :
+ {
+ *(.rodata) /* Read-only data */
+ *(.rodata.*)
+ } > ROM
- /* Uninitialized data section */
- .bss :
- {
- _sbss = .;
- KEEP(*(.bss))
- KEEP(*(COMMON))
- _ebss = .;
- } > RAM
+ .data :
+ {
+ _sidata = LOADADDR(.data); /* Start address of .data section in ROM */
+ . = ALIGN(4);
+ _sdata = .; /* Start of .data section in RAM */
+ *(.data) /* .data sections (initialized data) */
+ *(.data.*)
+ *(.sdata) /* .sdata sections (small initialized data) */
+ *(.sdata.*)
+ . = ALIGN(4);
+ _edata = .; /* End of .data section in RAM */
+ } > RAM AT > ROM
- /* Stack section */
- .stack (NOLOAD) :
- {
- _stack_start = .;
- . += 0x100; /* Adjust the size as needed */
- _stack_end = .;
- } > RAM
+ .bss :
+ {
+ . = ALIGN(4);
+ _sbss = .; /* Start of .bss section */
+ *(.bss) /* .bss sections (zero-initialized data) */
+ *(.bss.*)
+ *(.sbss) /* .sbss sections (small zero-initialized data) */
+ *(.sbss.*)
+ *(COMMON)
+ . = ALIGN(4);
+ _ebss = .; /* End of .bss section */
+ } > RAM
- /* Heap section */
- .heap (NOLOAD) :
- {
- _heap_start = .;
- . += 0x100; /* Adjust the size as needed */
- _heap_end = .;
- } > RAM
-}
+ .heap (NOLOAD) :
+ {
+ . = ALIGN(4);
+ _sheap = .; /* Start of heap */
+ . = . + 1K; /* Size of heap (adjust as needed) */
+ _eheap = .; /* End of heap */
+ . = ALIGN(4);
+ } > RAM
-/* Define symbols for memory initialization */
-PROVIDE(_start = 0x00010000);
-PROVIDE(_etext = _etext);
-PROVIDE(_sdata = _sdata);
-PROVIDE(_edata = _edata);
-PROVIDE(_sbss = _sbss);
-PROVIDE(_ebss = _ebss);
-PROVIDE(_sstack = _sstack);
-PROVIDE(_estack = _estack); \ No newline at end of file
+ .stack (NOLOAD) :
+ {
+ . = ALIGN(4);
+ _sstack = .; /* Start of stack */
+ . = . + 1K; /* Size of stack (adjust as needed) */
+ _estack = .; /* End of stack */
+ . = ALIGN(4);
+ } > RAM
+
+ /DISCARD/ :
+ {
+ *(.comment)
+ *(.note)
+ *(.note.*)
+ }
+}
diff --git a/prog/src/main.c b/prog/src/main.c
index 082172d..a31196f 100644
--- a/prog/src/main.c
+++ b/prog/src/main.c
@@ -1,13 +1,12 @@
#include <stdint.h>
-//volatile uint32_t *io_in = (volatile uint32_t *)0x00000000;
-//volatile uint32_t *io_out = (volatile uint32_t *)0x00000004;
+volatile uint32_t *io_in = (volatile uint32_t *)0x00000000;
+volatile uint32_t *io_out = (volatile uint32_t *)0x00000004;
+
int main(void) {
while (1) {
- for (int i = 0; i < 32; ++i) {
- *((volatile uint32_t *)0x00000004) = i;
- for (int j = 0; j < 1024 * 128; ++j);
- }
+ if (*io_in) *io_out = 0b00000;
+ else *io_out = 0b11111;
}
}
diff --git a/prog/src/prog.s b/prog/src/prog.s.tst
index ec227a9..ec227a9 100644
--- a/prog/src/prog.s
+++ b/prog/src/prog.s.tst
diff --git a/prog/src/startup.c b/prog/src/startup.c
new file mode 100644
index 0000000..32aa131
--- /dev/null
+++ b/prog/src/startup.c
@@ -0,0 +1,39 @@
+extern unsigned int _sidata; /* Start address for the .data section in ROM */
+extern unsigned int _sdata; /* Start address for the .data section in RAM */
+extern unsigned int _edata; /* End address for the .data section in RAM */
+extern unsigned int _sbss; /* Start address for the .bss section */
+extern unsigned int _ebss; /* End address for the .bss section */
+extern unsigned int _estack; /* End address for the stack section */
+
+void main(void); /* The main function declaration */
+void _start(void) __attribute__((section(".text.startup"), naked)); /* The entry point */
+
+void _start(void)
+{
+ unsigned int *src, *dest;
+
+ /* Copy .data section from ROM to RAM */
+ src = &_sidata; /* ROM address of the .data section */
+ for (dest = &_sdata; dest < &_edata;)
+ {
+ *dest++ = *src++;
+ }
+
+ //while(1);
+
+ /* Zero initialize .bss section */
+ for (dest = &_sbss; dest < &_ebss;)
+ {
+ *dest++ = 0;
+ }
+
+
+ /* Initialize stack pointer */
+ asm volatile ("la sp, _estack");
+
+ /* Call the main function */
+ main();
+
+ /* If main returns, loop forever */
+ while (1);
+}
diff --git a/prog/src/startup.s b/prog/src/startup.s
deleted file mode 100644
index 963c288..0000000
--- a/prog/src/startup.s
+++ /dev/null
@@ -1,59 +0,0 @@
-.section .init
-.globl _start
-_start:
-
-prog:
-/*
- la t0, 4
- la t1, 1
-
- sw t1, 0(t0)
-
- la t0, 0x00100000
- li t1, 1
- sw t1, 0(t0)
- lw t2, 0(t0)
-
-
- j halt
-
-*/
- /* Set up stack pointer */
- la sp, _stack_end
-
- /* Initialize .data section (copy from LMA to VMA) */
- la t0, _sdata /* VMA start of .data */
- la t1, _edata /* VMA end of .data */
- la t2, _etext /* LMA start of .data in flash */
-copy_data:
- beq t0, t1, clear_bss
- lw t3, 0(t2)
- sw t3, 0(t0)
- addi t0, t0, 4
- addi t2, t2, 4
- j copy_data
-
-clear_bss:
- /* Clear .bss section */
- la t0, _sbss /* VMA start of .bss */
- la t1, _ebss /* VMA end of .bss */
-clear_loop:
- beq t0, t1, init_libc
- sw x0, 0(t0)
- addi t0, t0, 4
- j clear_loop
-
-init_libc:
- /* Call libc initialization functions */
- /* call _init */
-
- /* Call main function */
- call main
-
-finalize_libc:
- /* Call libc finalization functions */
- /* call _fini */
-
-halt:
- /* Infinite loop after main returns */
- j halt
diff --git a/src/pc_reg.v b/src/pc_reg.v
index 2bdb540..11fe9ba 100644
--- a/src/pc_reg.v
+++ b/src/pc_reg.v
@@ -8,7 +8,7 @@ module pc_reg (
output reg [31:0] pc
);
-parameter PC_INITIAL = 32'h0001_0000;
+`include "include/consts.vh"
always @ (posedge clk or negedge rstn) begin
if (!rstn) pc <= PC_INITIAL;
diff --git a/src/reset_synchronizer.v b/src/reset_synchronizer.v
new file mode 100644
index 0000000..b957a4b
--- /dev/null
+++ b/src/reset_synchronizer.v
@@ -0,0 +1,22 @@
+module reset_synchronizer (
+ input clk,
+ input rstn_async,
+ output rstn
+);
+
+reg rstn_meta;
+reg rstn_sync_reg;
+
+always @(posedge clk or negedge rstn_async) begin
+ if (!rstn_async) begin
+ rstn_meta <= 1'b0;
+ rstn_sync_reg <= 1'b0;
+ end else begin
+ rstn_meta <= 1'b1;
+ rstn_sync_reg <= rstn_meta;
+ end
+end
+
+assign rstn = rstn_sync_reg;
+
+endmodule \ No newline at end of file
diff --git a/src/top.v b/src/top.v
index fc1d9fe..b10fbca 100644
--- a/src/top.v
+++ b/src/top.v
@@ -1,23 +1,31 @@
module top (
input clk,
input key,
+ input rst,
output [5:0] led
);
-wire rstn, clk_cpu;
-assign rstn = key;
+wire rstn, rstn_async, clk_cpu;
+assign rstn_async = rst;
wire [31:0] io_in;
wire [31:0] io_out;
+reset_synchronizer reset_synchronizer (
+ .clk(clk),
+ .rstn_async(rstn_async),
+ .rstn(rstn)
+);
+
clock_divider #(.N(1)) clkdiv (
- .clk(clk),
- .rstn(rstn),
- .clk_div(clk_cpu)
+ .clk(clk),
+ .rstn(rstn),
+ .clk_div(clk_cpu)
);
assign led[0] = ~clk_cpu;
assign led[5:1] = ~io_out[4:0];
+assign io_in[0] = key;
cpu cpu (