Skip to content

Commit

Permalink
libmenuet: Initial commit with example
Browse files Browse the repository at this point in the history
  • Loading branch information
ry755 committed May 27, 2023
1 parent a65e249 commit b82a204
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 19 deletions.
27 changes: 8 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ APPS := \
build/CACHE2FD \
build/CACHE2HD \
build/CALC \
build/CEXAMPLE \
build/COPY2 \
build/CPU \
build/CPUSPEED \
Expand Down Expand Up @@ -64,25 +65,13 @@ build/%: applications/%.ASM
mkdir -p build/
fasm $< $@

#build/KERNEL.MNT: kernel/KERNEL.ASM $(wildcard kernel/*.INC)
# mkdir -p $(MOUNT)
# mkdir -p build/
# sudo mount -o loop -t vfat $(IMAGE) $(MOUNT)
# fasm kernel/KERNEL.ASM build/KERNEL.MNT
# sudo cp build/KERNEL.MNT $(MOUNT)
# sleep 0.2
# sudo umount $(MOUNT)
# rm -rf $(MOUNT)
#
#build/%: applications/%.ASM
# mkdir -p $(MOUNT)
# mkdir -p build/
# sudo mount -o loop -t vfat $(IMAGE) $(MOUNT)
# fasm $< $@
# sudo cp $@ $(MOUNT)
# sleep 0.2
# sudo umount $(MOUNT)
# rm -rf $(MOUNT)
build/%: applications/%.c libmenuet/libmenuet.a
mkdir -p build/
smlrcc -I libmenuet/include -Wall -flat32 -origin 0 -o $@ libmenuet/libmenuet.a $<

libmenuet/libmenuet.a:
cd libmenuet && make

clean:
rm -rf build
cd libmenuet && make clean
34 changes: 34 additions & 0 deletions applications/CEXAMPLE.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "libmenuet.h"

#include <stdint.h>

void RedrawWindow();

void main() {
RedrawWindow();

while (true) {
uint32_t event = WaitForEvent();
switch (event) {
case 1: {
// redraw event
RedrawWindow();
break;
}

case 3: {
// button event
uint32_t button = GetButtonID() >> 8;
if (button == 1)
EndApplication();
}
}
}
}

void RedrawWindow() {
BeginRedraw();
DrawWindow(64, 64, 128, 128, 0xFFFFFF, "C TEST", NULL);
DisplayText(32, 32, 0x10000000, "hi");
EndRedraw();
}
1 change: 1 addition & 0 deletions libmenuet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
libmenuet.a
7 changes: 7 additions & 0 deletions libmenuet/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all: libmenuet.a

libmenuet.a: src/libmenuet.asm $(wildcard src/*.asm)
cd src && nasm -f elf -o ../$@ libmenuet.asm

clean:
rm -f libmenuet.a
39 changes: 39 additions & 0 deletions libmenuet/include/libmenuet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef LIBMENUET_H
#define LIBMENUET_H

#include <stdint.h>

#ifndef NULL
#define NULL ((void*) 0x0)
#endif

#ifndef bool
typedef int bool;
#endif

#ifndef true
#define true 0x1
#endif

#ifndef false
#define false 0x0
#endif

// application
void EndApplication();

// button
uint32_t GetButtonID();

// event
uint32_t WaitForEvent();
uint32_t CheckForEvent();

// window
void DrawWindow(uint16_t x_start, uint16_t y_start, uint16_t x_size, uint16_t y_size, uint32_t color, char *label, char *menu_struct);
void PutPixel(uint32_t x, uint32_t y, uint32_t color);
void DisplayText(uint16_t x, uint16_t y, uint32_t color, char *text);
void BeginRedraw();
void EndRedraw();

#endif
13 changes: 13 additions & 0 deletions libmenuet/src/application.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; void EndApplication();
global _EndApplication
_EndApplication:
push ebp
mov ebp, esp
pushad

mov eax, -1 ; syscall -1
int 0x40

popad
leave
ret
15 changes: 15 additions & 0 deletions libmenuet/src/button.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; uint32_t GetButtonID();
global _GetButtonID
_GetButtonID:
push ebp
mov ebp, esp
pushad

mov eax, 17 ; syscall 17
int 0x40
mov dword [return], eax

popad
leave
mov eax, dword [return]
ret
18 changes: 18 additions & 0 deletions libmenuet/src/crt0.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
bits 32

; __start must be here in order for the linker to not insert a jump before the Menuet header
global __start
__start:
db 'MENUET01' ; 8 byte id
dd 0x01 ; header version
dd actual_start ; program start
dd 0x00100000 ; program image size (FIXME: this should be the actual image size)
dd 0x00100000 ; required amount of memory
dd 0x0007F000 ; stack
dd 0x00, 0x00 ; param,icon

extern _main
actual_start:
call _main
call _EndApplication
jmp $
31 changes: 31 additions & 0 deletions libmenuet/src/event.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
; uint32_t WaitForEvent();
global _WaitForEvent
_WaitForEvent:
push ebp
mov ebp, esp
pushad

mov eax, 10 ; syscall 10
int 0x40
mov dword [return], eax

popad
leave
mov eax, dword [return]
ret

; uint32_t CheckForEvent();
global _CheckForEvent
_CheckForEvent:
push ebp
mov ebp, esp
pushad

mov eax, 11 ; syscall 11
int 0x40
mov dword [return], eax

popad
leave
mov eax, dword [return]
ret
10 changes: 10 additions & 0 deletions libmenuet/src/libmenuet.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bits 32

%include "crt0.asm"

%include "application.asm"
%include "button.asm"
%include "event.asm"
%include "window.asm"

return: dd 0x00000000
108 changes: 108 additions & 0 deletions libmenuet/src/window.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
; void DrawWindow(
; uint16_t x_start,
; uint16_t y_start,
; uint16_t x_size,
; uint16_t y_size,
; uint32_t color,
; char *label,
; char *menu_struct
; );
global _DrawWindow
_DrawWindow:
push ebp
mov ebp, esp
pushad

mov eax, 0 ; syscall 0
movzx ebx, word [ebp + 8] ; x_start
shl ebx, 16
mov bx, word [ebp + 16] ; x_size
movzx ecx, word [ebp + 12] ; y_start
shl ecx, 16
mov cx, word [ebp + 20] ; y_size
mov edx, dword [ebp + 24] ; color
and edx, 0x00FFFFFF
or edx, 0x04000000 ; "skinned window with menu"
mov esi, dword [ebp + 28] ; label
mov edi, dword [ebp + 32] ; menu_struct
int 0x40

popad
leave
ret

; void PutPixel(
; uint32_t x,
; uint32_t y,
; uint32_t color
; );
global _PutPixel
_PutPixel:
push ebp
mov ebp, esp
pushad

mov eax, 1 ; syscall 1
mov ebx, dword [ebp + 8] ; x
mov ecx, dword [ebp + 12] ; y
mov edx, dword [ebp + 16] ; color
int 0x40

popad
leave
ret

; void DisplayText(
; uint16_t x,
; uint16_t y,
; uint32_t color,
; char *text
; );
global _DisplayText
_DisplayText:
push ebp
mov ebp, esp
pushad

mov eax, 4 ; syscall 4
movzx ebx, word [ebp + 8] ; x
shl ebx, 16
mov bx, word [ebp + 12] ; y
mov ecx, dword [ebp + 16] ; color
mov edx, dword [ebp + 20] ; text
mov esi, -1 ; null-terminated string
int 0x40

popad
leave
ret

; void BeginRedraw();
global _BeginRedraw
_BeginRedraw:
push ebp
mov ebp, esp
pushad

mov eax, 12 ; syscall 12
mov ebx, 1 ; begin redraw
int 0x40

popad
leave
ret

; void EndRedraw();
global _EndRedraw
_EndRedraw:
push ebp
mov ebp, esp
pushad

mov eax, 12 ; syscall 12
mov ebx, 2 ; end redraw
int 0x40

popad
leave
ret

0 comments on commit b82a204

Please sign in to comment.