diff options
author | Marvin Borner | 2021-04-19 22:38:59 +0200 |
---|---|---|
committer | Marvin Borner | 2021-04-19 22:38:59 +0200 |
commit | db55ea657e1edcb5d7da3dd90e4dc6b7643bbfe0 (patch) | |
tree | 3f9b26c54bf3bfafac2e30c82b67bba02883d5c1 |
8051 is awesome
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 21 | ||||
-rw-r--r-- | src/gui.c | 121 | ||||
-rw-r--r-- | src/inc/def.h | 14 | ||||
-rw-r--r-- | src/inc/gui.h | 8 | ||||
-rw-r--r-- | src/inc/lexer.h | 64 | ||||
-rw-r--r-- | src/inc/parser.h | 8 | ||||
-rw-r--r-- | src/main.c | 18 | ||||
-rw-r--r-- | src/parser.c | 24 | ||||
-rw-r--r-- | test.asm | 2 |
10 files changed, 281 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..992b8b2 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +SOURCEDIR = src +BUILDDIR = build +SOURCES = $(wildcard $(SOURCEDIR)/*.c) +OBJS = $(patsubst $(SOURCEDIR)/%.c, $(BUILDDIR)/%.o, $(SOURCES)) + +CC = gcc +WARNINGS = -Wall -Wextra -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wlogical-op -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic +CFLAGS = -O3 $(WARNINGS) -I$(SOURCEDIR)/inc/ $(shell pkg-config --cflags --libs gtk+-3.0) + +all: $(OBJS) + @$(CC) -o ./$(BUILDDIR)/out $^ $(CFLAGS) + +clean: + @$(RM) -rf $(BUILDDIR) + +run: clean all + @./$(BUILDDIR)/out + +$(BUILDDIR)/%.o: $(SOURCEDIR)/%.c + @mkdir -p $(BUILDDIR) + @$(CC) -c -o $@ $< $(CFLAGS) diff --git a/src/gui.c b/src/gui.c new file mode 100644 index 0000000..7564f11 --- /dev/null +++ b/src/gui.c @@ -0,0 +1,121 @@ +#include <def.h> +#include <gtk/gtk.h> +#include <gui.h> +#include <parser.h> +#include <stdio.h> +#include <string.h> + +// Main window +static GtkWidget *window; +static GtkWidget *text_view; + +static char *gui_text_buffer(void) +{ + GtkTextIter start, end; + GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view)); + + gtk_text_buffer_get_bounds(buffer, &start, &end); + gchar *text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE); + + return text; // g_free after use! +} + +static void gui_fill_text_view(const char *path) +{ + char buf[1024] = { 0 }; + FILE *test = fopen(path, "r"); + + if (!test) { + gui_show_warning(strerror(errno)); + return; + } + + fread(buf, sizeof(buf), sizeof(buf[0]), test); + fclose(test); + + GtkTextBuffer *text_buffer = gtk_text_buffer_new(NULL); + gtk_text_buffer_set_text(text_buffer, buf, strnlen(buf, sizeof(buf))); + gtk_text_view_set_buffer(GTK_TEXT_VIEW(text_view), text_buffer); +} + +static void gui_call_parser(GtkWidget *widget, gpointer data) +{ + UNUSED(data); + UNUSED(widget); + + char *buf = gui_text_buffer(); + + // TODO: Get actual buffer size + if (parse(buf, 1024)) + gui_show_info("Compiled successfully!"); + + g_free(buf); +} + +void gui_show_warning(const char *text) +{ + if (!text) + return; + + GtkWidget *warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, + GTK_BUTTONS_OK, "Warning"); + + gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(warning), "%s\n", text); + int response = gtk_dialog_run(GTK_DIALOG(warning)); + printf("%d\n", response); + gtk_widget_destroy(warning); +} + +void gui_show_info(const char *text) +{ + if (!text) + return; + + GtkWidget *info = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, + GTK_BUTTONS_OK, "Information"); + + gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(info), "%s\n", text); + int response = gtk_dialog_run(GTK_DIALOG(info)); + printf("%d\n", response); + gtk_widget_destroy(info); +} + +static void gui_activate(GtkApplication *app, gpointer data) +{ + UNUSED(data); + + window = gtk_application_window_new(app); + gtk_window_set_title(GTK_WINDOW(window), "Window"); + gtk_window_set_default_size(GTK_WINDOW(window), 200, 200); + + GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 16); + gtk_container_add(GTK_CONTAINER(window), box); + + text_view = gtk_text_view_new(); + gtk_container_add(GTK_CONTAINER(box), text_view); + + GtkWidget *button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL); + gtk_container_add(GTK_CONTAINER(box), button_box); + + GtkWidget *button = gtk_button_new_with_label("Compile"); + g_signal_connect(button, "clicked", G_CALLBACK(gui_call_parser), NULL); + gtk_container_add(GTK_CONTAINER(button_box), button); + + gtk_widget_show_all(window); + + // Only for testing purposes + gui_fill_text_view("test.asm"); +} + +int gui_init(int argc, char *argv[]) +{ + gtk_init(&argc, &argv); + + GtkApplication *app = + gtk_application_new("de.melvars.simsalasim", G_APPLICATION_FLAGS_NONE); + g_signal_connect(app, "activate", G_CALLBACK(gui_activate), NULL); + int status = g_application_run(G_APPLICATION(app), argc, argv); + g_object_unref(app); + + return status; +} diff --git a/src/inc/def.h b/src/inc/def.h new file mode 100644 index 0000000..bd06eb9 --- /dev/null +++ b/src/inc/def.h @@ -0,0 +1,14 @@ +#ifndef DEF_H +#define DEF_H + +typedef unsigned int u32; +typedef unsigned short u16; +typedef unsigned char u8; + +typedef signed int s32; +typedef signed short s16; +typedef signed char s8; + +#define UNUSED(bla) ((void)(bla)) + +#endif diff --git a/src/inc/gui.h b/src/inc/gui.h new file mode 100644 index 0000000..1c7fc01 --- /dev/null +++ b/src/inc/gui.h @@ -0,0 +1,8 @@ +#ifndef GUI_H +#define GUI_H + +int gui_init(int argc, char *argv[]); +void gui_show_warning(const char *text); +void gui_show_info(const char *text); + +#endif diff --git a/src/inc/lexer.h b/src/inc/lexer.h new file mode 100644 index 0000000..833e522 --- /dev/null +++ b/src/inc/lexer.h @@ -0,0 +1,64 @@ +#ifndef LEXER_H +#define LEXER_H + +enum token { + UNKNOWN, + NEWLINE, + NOP, + JBC, + JB, + JNB, + JC, + JNC, + JZ, + JNZ, + SJMP, + MOV, + ORL, + ANL, + PUSH, + POP, + MOVX, + AJMP, + ACALL, + LJMP, + LCALL, + RETI, + RET, + XRL, + CPL, + CLR, + SETB, + RR, + RRC, + RL, + RLC, + XLR, + JMP, + MOVC, + INC, + DEC, + ADD, + ADDC, + DIV, + DUBB, + MUL, + CJNE, + SWP, + DA, + CRL, + XCH, + DJNZ, + XCHD, + CALL, + ORG, + DB, + DW, + INCLUDE, + BRACE_OPEN, + BRACE_CLOSE, + DATA, + BIT, +}; + +#endif diff --git a/src/inc/parser.h b/src/inc/parser.h new file mode 100644 index 0000000..c46ead1 --- /dev/null +++ b/src/inc/parser.h @@ -0,0 +1,8 @@ +#ifndef LEXER_H +#define LEXER_H + +#include <def.h> + +u8 parse(char *buf, u32 size); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..74cfe24 --- /dev/null +++ b/src/main.c @@ -0,0 +1,18 @@ +#include <gui.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static void print_help(void) +{ + printf("Help\n"); + exit(0); +} + +int main(int argc, char *argv[]) +{ + if (argc == 2 && (strcmp(argv[1], "--help") == 0)) + print_help(); + + return gui_init(argc, argv); +} diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..e4d0251 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,24 @@ +#include <def.h> +#include <parser.h> +#include <stdio.h> +#include <string.h> + +#define PEEK(hay, needle) (strcmp(hay, needle) == 0) + +u8 parse(char *buf, u32 size) +{ + u32 line = 0; + + for (u32 i = 0; i < size; i++) { + /* printf("'%c'\n", buf[i]); */ + if (buf[i] == '\0') + break; + + if (buf[i] == '\n') { + line++; + continue; + } + } + + return 1; +} diff --git a/test.asm b/test.asm new file mode 100644 index 0000000..95063e7 --- /dev/null +++ b/test.asm @@ -0,0 +1,2 @@ +mov A, #42 +jmp $ |