Skip to content

Commit 8f4d603

Browse files
committed
fix: fix simulations
1 parent 6161466 commit 8f4d603

17 files changed

Lines changed: 350 additions & 198 deletions

example_asm/cycle_counter.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
#include "header.h"
2+
13
// should run in SOC
24
void exception_handler();
35

4-
__attribute__((section(
5-
".bootinfo"))) volatile const unsigned long long exception_handler_addr =
6-
(unsigned long long)(void *)&exception_handler;
76
const static unsigned int step = 12; // should be >= 12
87

8+
DEFAULT_HEADER_DEFINITIONS;
9+
910
__asm__(".section .text\n"
1011
"li $sp, _stack_top\n"
1112
"li $gp, __global_pointer$\n"

example_asm/fibonacci.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1+
#include "header.h"
12
#include "optnone.h"
23

34
void exception_handler();
45

5-
__attribute__((section(
6-
".bootinfo"))) volatile const unsigned long long exception_handler_addr =
7-
(unsigned long long)(void *)&exception_handler;
86
const static unsigned int step = 6;
97

10-
__asm__(".section .text\n"
11-
"li $sp, _stack_top\n"
12-
"li $gp, __global_pointer$\n"
13-
"li $ra, 0x0d00\n" // placeholder
14-
"li $t9, __start\n"
15-
"jr $t9\n");
8+
DEFAULT_HEADER_DEFINITIONS;
169

1710
NONOPT void __start() {
1811
unsigned int prev = 0, curr = 1, sum = 0;

example_asm/header.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#define DEFINE_EXCEPTION \
2+
__asm__(".section .bootinfo,\"ax\"\n" \
3+
".balign 8\n" \
4+
"exception_handler_addr:\n" \
5+
"jal exception_handler\n" \
6+
"nop\n" \
7+
".text\n")
8+
9+
#define DEFINE_ENTRY_POINT \
10+
__asm__(".section .text\n" \
11+
"li $sp, _stack_top\n" \
12+
"li $gp, _gp\n" \
13+
"li $ra, 0x0d00\n" /* placeholder */ \
14+
"li $t9, __start\n" \
15+
"jr $t9\n")
16+
17+
#define DEFAULT_HEADER_DEFINITIONS \
18+
DEFINE_EXCEPTION; \
19+
DEFINE_ENTRY_POINT

example_asm/hello_world.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1+
#include "header.h"
12
#include <stdint.h>
23

34
void exception_handler();
45

5-
__attribute__((unused, section(".bootinfo")))
6-
const uint64_t exception_handler_addr = (uint64_t)(void *)&exception_handler;
7-
86
static char *const stdout_mmio = (char *)(0x20000010);
97
static const uint64_t stdout_buffer_size = 8;
108

11-
__asm__(".section .text\n"
12-
"li $sp, _stack_top\n"
13-
"li $gp, __global_pointer$\n"
14-
"li $ra, 0x0d00\n" // placeholder
15-
"li $t9, __start\n"
16-
"jr $t9\n");
9+
DEFAULT_HEADER_DEFINITIONS;
1710

1811
void puts_(const char *str) {
1912
while (str[0]) {

simulations/CMakeLists.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(VGA_SRCS
99
set(SDL3_ENABLED SOC_VGA VGA)
1010
set(CAPSTONE_DISABLED SOC_run)
1111
set(SOC_run_C_flag -O3 -flto)
12+
set(VERILATOR_ARGS +define+MOCK_STDOUT_ENABLED)
1213
set(sim_VCD_dump OFF CACHE BOOL "Enable or disable VCD dump in simulation targets")
1314

1415
find_package(SDL3)
@@ -38,13 +39,18 @@ foreach(sim_src IN LISTS SIM_SOURCES)
3839
if(${target}_linker_flags)
3940
set_target_properties(${target_name} PROPERTIES LINK_FLAGS "${${target}_linker_flags}")
4041
endif()
42+
if(target IN_LIST SDL3_ENABLED)
43+
set(LOCAL_VERILATOR_ARGS ${VERILATOR_ARGS} +define+VGA_ENABLED)
44+
else()
45+
set(LOCAL_VERILATOR_ARGS ${VERILATOR_ARGS})
46+
endif()
4147

4248
if(sim_VCD_dump)
4349
verilate(${target_name}
4450
SOURCES ${${srclist}}
4551
TOP_MODULE ${base_target}
4652
PREFIX ${unitCap}_sim
47-
VERILATOR_ARGS
53+
VERILATOR_ARGS ${LOCAL_VERILATOR_ARGS}
4854
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${target}
4955
TRACE_VCD
5056
)
@@ -54,7 +60,7 @@ foreach(sim_src IN LISTS SIM_SOURCES)
5460
SOURCES ${${srclist}}
5561
TOP_MODULE ${base_target}
5662
PREFIX ${unitCap}_sim
57-
VERILATOR_ARGS
63+
VERILATOR_ARGS ${LOCAL_VERILATOR_ARGS}
5864
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${target}
5965
)
6066
endif()
@@ -92,10 +98,9 @@ foreach(debug_src IN LISTS DEBUG_SOURCES)
9298
SOURCES ${${srclist}}
9399
TOP_MODULE ${base_target}
94100
PREFIX ${unitCap}_debug
95-
VERILATOR_ARGS
96101
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${target}
97102
TRACE_VCD
98-
VERILATOR_ARGS +define+DEBUG
103+
VERILATOR_ARGS +define+DEBUG ${VERILATOR_ARGS}
99104
)
100105
target_link_libraries(${target_name} PRIVATE ${CAPSTONE_LIBRARIES})
101106
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${target} ${CAPSTONE_INCLUDE_DIRS})
@@ -120,9 +125,8 @@ else()
120125
SOURCES ${SOC_SRCS}
121126
TOP_MODULE SOC
122127
PREFIX SOC_sim
123-
VERILATOR_ARGS
124128
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/debugger_tui
125-
VERILATOR_ARGS +define+DEBUGGER
129+
VERILATOR_ARGS +define+DEBUGGER ${VERILATOR_ARGS}
126130
)
127131

128132
target_include_directories(debugger_tui

simulations/SOC_VGA_sim.cpp

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
#include <SDL3/SDL_main.h>
44
#include <SOC_VGA_sim.h>
55
#include <SOC_VGA_sim__Syms.h>
6+
#include <SOC_VGA_sim_data_mem.h>
67
#include <verilated.h>
78

89
#include <iostream>
910

10-
constexpr int SCREEN_WIDTH = 640;
11-
constexpr int SCREEN_HEIGHT = 480;
12-
constexpr int SCALE_FACTOR = 5;
13-
constexpr int BUF_WIDTH = 640 / SCALE_FACTOR;
14-
constexpr int BUF_HEIGHT = 480 / SCALE_FACTOR;
11+
constexpr int SCREEN_WIDTH = 640;
12+
constexpr int SCREEN_HEIGHT = 480;
13+
constexpr int SCALE_FACTOR = 5;
14+
constexpr int BUF_WIDTH = 640 / SCALE_FACTOR;
15+
constexpr int BUF_HEIGHT = 480 / SCALE_FACTOR;
1516
constexpr size_t VGA_COLOR_ADDR = 0x20000008;
1617
// VGA clk speed vs system clk speed
1718
constexpr unsigned int VGA_CLK_V_CLK = 4;
@@ -20,9 +21,7 @@ uint32_t fps_lasttime = SDL_GetTicks();
2021
uint32_t fps_current;
2122
uint32_t fps_frames = 0;
2223

23-
inline uint8_t color4to8(uint8_t c) {
24-
return c << 4 | c;
25-
}
24+
inline uint8_t color4to8(uint8_t c) { return c << 4 | c; }
2625

2726
class Pixel {
2827
public:
@@ -36,89 +35,96 @@ class Pixel {
3635
};
3736

3837
struct AppState {
39-
SDL_Window* window;
40-
SDL_Renderer* renderer;
41-
SOC_VGA_sim* top;
42-
VerilatedContext* ctx;
38+
SDL_Window *window;
39+
SDL_Renderer *renderer;
40+
SOC_VGA_sim *top;
41+
VerilatedContext *ctx;
4342
};
4443

45-
#define TICK \
46-
as->top->clk = !as->top->clk; \
47-
if (as->ctx->time() % VGA_CLK_V_CLK == 0) as->top->VGA_clk = !as->top->VGA_clk; \
48-
as->ctx->timeInc(1); \
44+
#define TICK \
45+
as->top->sys_clk = !as->top->sys_clk; \
46+
if (as->ctx->time() % VGA_CLK_V_CLK == 0) \
47+
as->top->VGA_clk = !as->top->VGA_clk; \
48+
as->ctx->timeInc(1); \
4949
as->top->eval()
5050

51-
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
51+
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
5252
if (!SDL_SetAppMetadata("VOC_sim", nullptr, nullptr)) {
5353
return SDL_APP_FAILURE;
5454
}
5555

5656
Verilated::commandArgs(argc, argv);
5757

5858
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
59-
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
59+
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
60+
<< std::endl;
6061
return SDL_APP_FAILURE;
6162
}
6263

63-
AppState* as = (AppState*)SDL_calloc(1, sizeof(AppState));
64+
AppState *as = (AppState *)SDL_calloc(1, sizeof(AppState));
6465
if (!as) {
6566
return SDL_APP_FAILURE;
6667
}
6768

6869
*appstate = as;
6970

70-
if (!SDL_CreateWindowAndRenderer(nullptr, SCREEN_WIDTH, SCREEN_HEIGHT, 0, &as->window,
71-
&as->renderer)) {
72-
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
71+
if (!SDL_CreateWindowAndRenderer(nullptr, SCREEN_WIDTH, SCREEN_HEIGHT, 0,
72+
&as->window, &as->renderer)) {
73+
std::cerr << "Window could not be created! SDL_Error: "
74+
<< SDL_GetError() << std::endl;
7375
return SDL_APP_FAILURE;
7476
}
7577

76-
as->ctx = new VerilatedContext;
77-
as->top = new SOC_VGA_sim{as->ctx};
78-
as->top->reset = 1;
79-
as->top->clk = 1;
78+
as->ctx = new VerilatedContext;
79+
as->top = new SOC_VGA_sim{as->ctx};
80+
as->top->sys_rst_n = 0;
81+
as->top->sys_clk = 1;
8082
as->top->VGA_clk = 1;
8183
TICK;
8284
TICK;
83-
as->top->reset = 0;
85+
as->top->sys_rst_n = 1;
8486

85-
std::cout << "simulation starting, press 'q' to quit, 'r' to reset." << std::endl;
87+
std::cout << "simulation starting, press 'q' to quit, 'r' to reset."
88+
<< std::endl;
8689

8790
return SDL_APP_CONTINUE;
8891
}
8992

90-
SDL_AppResult SDL_AppIterate(void* appstate) {
91-
AppState* as = (AppState*)appstate;
93+
SDL_AppResult SDL_AppIterate(void *appstate) {
94+
AppState *as = (AppState *)appstate;
9295
static uint32_t frame_counter = 0;
9396

9497
if (as->ctx->time() % VGA_CLK_V_CLK == 0 && as->top->VGA_clk &&
95-
as->top->SOC->vga->h < SCREEN_WIDTH && as->top->SOC->vga->v < SCREEN_HEIGHT) {
96-
SDL_SetRenderDrawColor(as->renderer, color4to8(as->top->VGA_r), color4to8(as->top->VGA_g),
98+
as->top->SOC->vga->h < SCREEN_WIDTH &&
99+
as->top->SOC->vga->v < SCREEN_HEIGHT) {
100+
SDL_SetRenderDrawColor(as->renderer, color4to8(as->top->VGA_r),
101+
color4to8(as->top->VGA_g),
97102
color4to8(as->top->VGA_b), SDL_ALPHA_OPAQUE);
98-
SDL_RenderPoint(as->renderer, as->top->SOC->vga->h, as->top->SOC->vga->v);
103+
SDL_RenderPoint(as->renderer, as->top->SOC->vga->h,
104+
as->top->SOC->vga->v);
99105

100106
if (as->top->SOC->vga->h == 0 && as->top->SOC->vga->v == 0) {
101107
SDL_RenderPresent(as->renderer);
102108
fps_frames++;
103109
frame_counter++;
104110
if (fps_lasttime < SDL_GetTicks() - 1000) {
105111
fps_lasttime = SDL_GetTicks();
106-
fps_current = fps_frames;
112+
fps_current = fps_frames;
107113
std::cout << "current FPS: " << fps_current << std::endl;
108114
fps_frames = 0;
109115
}
110116
}
111117
}
112118
if (as->top->SOC->stdout->stdout_taken) {
113-
printf("stdout: %s \n", (const char*)&as->top->SOC->stdout->buffer);
119+
printf("stdout: %s \n", (const char *)&as->top->SOC->stdout->buffer);
114120
}
115121
TICK;
116122
TICK;
117123
return SDL_APP_CONTINUE;
118124
}
119125

120-
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
121-
AppState* as = (AppState*)appstate;
126+
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
127+
AppState *as = (AppState *)appstate;
122128
switch (event->type) {
123129
case SDL_EVENT_QUIT:
124130
return SDL_APP_SUCCESS;
@@ -139,9 +145,9 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
139145
return SDL_APP_CONTINUE;
140146
}
141147

142-
void SDL_AppQuit(void* appstate, SDL_AppResult result) {
148+
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
143149
if (appstate != NULL) {
144-
AppState* as = (AppState*)appstate;
150+
AppState *as = (AppState *)appstate;
145151
delete as->top;
146152
delete as->ctx;
147153
SDL_DestroyRenderer(as->renderer);

simulations/SOC_debug.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include <SOC_debug.h>
22
#include <SOC_debug_SOC.h>
33
#include <SOC_debug_core.h>
4+
#include <SOC_debug_core_ID.h>
5+
#include <SOC_debug_core_IF.h>
46
#include <SOC_debug_core_MEM.h>
57
#include <SOC_debug_cp0.h>
68
#include <SOC_debug_data_mem.h>
79
#include <SOC_debug_stdout.h>
10+
#include <SOC_debug_xpm_memory_sdpram__pi1.h>
811
#include <verilated.h>
912
#include <verilated_vcd_c.h>
1013

@@ -47,10 +50,10 @@ int main(int argc, char **argv) {
4750
// return -1;
4851
// }
4952

50-
machine->clk = 1;
51-
machine->reset = 1;
53+
machine->sys_clk = 1;
54+
machine->sys_rst_n = 0;
5255
TICK;
53-
machine->reset = 0;
56+
machine->sys_rst_n = 1;
5457
std::cout << "Press any key to step." << std::endl;
5558
mainLoop(machine, ctx, cycle_max, cs_handle, disasm_cache, true);
5659
dumpMem(machine);

simulations/SOC_run_sim.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <SOC_run_sim_core.h>
44
#include <SOC_run_sim_core_MEM.h>
55
#include <SOC_run_sim_cp0.h>
6+
#include <SOC_run_sim_data_mem.h>
67
#include <SOC_run_sim_stdout.h>
78
#include <verilated.h>
89

@@ -24,7 +25,7 @@
2425

2526
#define TICK_HALF \
2627
do { \
27-
machine->clk = !machine->clk; \
28+
machine->sys_clk = !machine->sys_clk; \
2829
machine->eval(); \
2930
TICK_TRACE \
3031
ctx->timeInc(1); \
@@ -46,10 +47,10 @@ int main(int argc, char **argv) {
4647
tfp->open("trace.vcd");
4748
#endif
4849

49-
machine->clk = 1;
50-
machine->reset = 1;
50+
machine->sys_clk = 1;
51+
machine->sys_rst_n = 0;
5152
TICK;
52-
machine->reset = 0;
53+
machine->sys_rst_n = 1;
5354

5455
while (1) {
5556
if (machine->SOC->stdout->stdout_taken) {

simulations/SOC_sim.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <SOC_sim_cp0.h>
77
#include <SOC_sim_data_mem.h>
88
#include <SOC_sim_stdout.h>
9+
#include <SOC_sim_xpm_memory_sdpram__pi1.h>
910
#include <verilated.h>
1011
#include <verilated_vcd_c.h>
1112

@@ -40,10 +41,10 @@ int main(int argc, char **argv) {
4041
// return -1;
4142
// }
4243

43-
machine->clk = 1;
44-
machine->reset = 1;
44+
machine->sys_clk = 1;
45+
machine->sys_rst_n = 0;
4546
TICK;
46-
machine->reset = 0;
47+
machine->sys_rst_n = 1;
4748
mainLoop(machine, ctx, cycle_max, cs_handle, disasm_cache);
4849
dumpMem(machine);
4950

0 commit comments

Comments
 (0)