Skip to content

Commit 577c9d0

Browse files
committed
Added miniature simulator using simavr
1 parent e0898ee commit 577c9d0

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

sim/Makefile

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Makefile for kos simulation
2+
#
3+
# The basic simulation is just a flashing led
4+
#
5+
6+
BINDIR=../bin
7+
OBJDIR=obj
8+
SRCDIR=src
9+
INCDIR=include
10+
11+
INCLUDES=-I$(INCDIR)
12+
LIBS=simavr sdl2
13+
14+
CC=gcc
15+
MKDIR=mkdir
16+
RM=rm -rf
17+
18+
SRC=$(wildcard $(SRCDIR)/*.c)
19+
OBJS:=$(addprefix $(OBJDIR)/,$(notdir $(SRC:.c=.o)))
20+
21+
CFLAGS=$(INCLUDES) -g
22+
CFLAGS+=`pkg-config --cflags $(LIBS)`
23+
LDFLAGS=`pkg-config --libs $(LIBS)` -lpthread
24+
25+
all:: $(BINDIR)/sim
26+
27+
clean:
28+
$(RM) $(BINDIR)/sim
29+
$(RM) $(OBJDIR)
30+
31+
$(BINDIR)/sim: $(OBJS)
32+
@$(MKDIR) -p $(dir $@)
33+
$(CC) $(LDFLAGS) $(OBJS) -o $@
34+
35+
$(OBJDIR)/%.o: $(SRCDIR)/%.c
36+
@$(MKDIR) -p $(dir $@)
37+
$(CC) $(CFLAGS) -c $< -o $@
38+

sim/src/main.c

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Simulator for KOS using sdl
3+
*/
4+
5+
#include <stdio.h>
6+
#include <libgen.h>
7+
8+
#include <pthread.h>
9+
10+
#include <SDL.h>
11+
12+
#include "sim_avr.h"
13+
#include "sim_elf.h"
14+
#include "sim_gdb.h"
15+
16+
#define GDB_PORT 1234
17+
#define SCREEN_WIDTH 640
18+
#define SCREEN_HEIGHT 480
19+
20+
static avr_t *avr;
21+
22+
static void *avr_run_thread(void * ig)
23+
{
24+
while (1)
25+
{
26+
avr_run(avr);
27+
}
28+
29+
return NULL;
30+
}
31+
32+
static SDL_Window *init_window(void)
33+
{
34+
SDL_Window *win = 0;
35+
36+
if (SDL_Init(SDL_INIT_VIDEO) < 0)
37+
{
38+
fprintf(stderr, "Unable to initialize SDL: %s", SDL_GetError());
39+
exit(1);
40+
}
41+
42+
win = SDL_CreateWindow("KOS Simulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
43+
if (!win)
44+
{
45+
fprintf(stderr, "Unable to create SDL Window: %s", SDL_GetError());
46+
exit(1);
47+
}
48+
49+
return win;
50+
}
51+
52+
static char handle_event(SDL_Event *event)
53+
{
54+
switch (event->type)
55+
{
56+
case SDL_QUIT:
57+
return 0;
58+
}
59+
60+
return 1; //continue
61+
}
62+
63+
int main(int argc, char** argv)
64+
{
65+
elf_firmware_t f;
66+
SDL_Window *win;
67+
const char *fname = "kos.elf";
68+
SDL_Event event;
69+
70+
if (elf_read_firmware(fname, &f) < 0)
71+
{
72+
exit(1);
73+
}
74+
75+
avr = avr_make_mcu_by_name(f.mmcu);
76+
avr_init(avr);
77+
avr_load_firmware(avr, &f);
78+
79+
if (SDL_Init(SDL_INIT_VIDEO) < 0)
80+
{
81+
fprintf(stderr, "Unable to initialize SDL: %s", SDL_GetError());
82+
exit(1);
83+
}
84+
85+
win = SDL_CreateWindow("KOS Simulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
86+
if (!win)
87+
{
88+
fprintf(stderr, "Unable to create SDL Window: %s", SDL_GetError());
89+
exit(1);
90+
}
91+
92+
while (SDL_WaitEvent(&event) >= 0)
93+
{
94+
if (!handle_event(&event))
95+
break;
96+
}
97+
98+
SDL_DestroyWindow(win);
99+
100+
SDL_Quit();
101+
102+
return 0;
103+
}

0 commit comments

Comments
 (0)