#include #include #include "Vcomputer.h" #include "verilated.h" #include "verilated_vcd_c.h" #include #include #include #define framerate 64.0 #define screen_width 640 #define screen_height 480 #define BLOCK_LEN 10 #define GRID_HEIGHT 8 #define GRID_WIDTH 8 #define GRID_BORDER_WIDTH 10 void draw_box(int x, int y, ALLEGRO_COLOR color){ al_draw_filled_rectangle(GRID_BORDER_WIDTH+BLOCK_LEN*x, GRID_BORDER_WIDTH+BLOCK_LEN*y, GRID_BORDER_WIDTH+BLOCK_LEN*x+BLOCK_LEN, GRID_BORDER_WIDTH+BLOCK_LEN*y+BLOCK_LEN, color); } int main(int argc, char **argv) { Verilated::commandArgs(argc, argv); Vcomputer *tb = new Vcomputer; unsigned int tickcount = 0; if(!al_init()) { printf("couldn't initialize allegro\n"); return 1; } if(!al_init_primitives_addon()){ printf("couldn't initialize primitives addon\n"); return 1; } if(!al_install_keyboard()) { printf("couldn't initialize keyboard\n"); return 1; } ALLEGRO_TIMER* timer = al_create_timer(1.0 / framerate); if(!timer) { printf("couldn't initialize timer\n"); return 1; } ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); if(!queue) { printf("couldn't initialize queue\n"); return 1; } ALLEGRO_DISPLAY* disp = al_create_display(screen_width, screen_height); if(!disp) { printf("couldn't initialize display\n"); return 1; } al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(disp)); al_register_event_source(queue, al_get_timer_event_source(timer)); bool done = false; bool redraw = true; ALLEGRO_EVENT event; al_start_timer(timer); int pixelCounter =0; while(1) { tb->eval(); al_wait_for_event(queue, &event); switch(event.type) { case ALLEGRO_EVENT_TIMER: // game logic goes here. redraw = true; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; } if(done) break; if(redraw && al_is_event_queue_empty(queue)) { al_draw_rectangle(0,0,2*GRID_BORDER_WIDTH+BLOCK_LEN*GRID_WIDTH, 2*GRID_BORDER_WIDTH+BLOCK_LEN*GRID_HEIGHT, al_map_rgb(0,0,255),GRID_BORDER_WIDTH); draw_box(pixelCounter%8,pixelCounter/8,al_map_rgb(255*tb->videoSignal,0,0)); pixelCounter++; if(pixelCounter>=64){ pixelCounter=0; al_flip_display(); } redraw = false; tb->clk = !tb->clk; tb->eval(); tb->clk = !tb->clk; } } al_destroy_display(disp); al_destroy_timer(timer); al_destroy_event_queue(queue); return 0; }