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 ;
1516constexpr size_t VGA_COLOR_ADDR = 0x20000008 ;
1617// VGA clk speed vs system clk speed
1718constexpr unsigned int VGA_CLK_V_CLK = 4 ;
@@ -20,9 +21,7 @@ uint32_t fps_lasttime = SDL_GetTicks();
2021uint32_t fps_current;
2122uint32_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
2726class Pixel {
2827 public:
@@ -36,89 +35,96 @@ class Pixel {
3635};
3736
3837struct 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 );
0 commit comments