-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscall.c
94 lines (89 loc) · 2.85 KB
/
syscall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdbool.h>
#include <avr/io.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <uzebox.h>
#include <string.h>
#include "flash.h"
#include "syscall.h"
#include "video.h"
extern u8 page_buffer[256];
extern u8 keyboard_buffer;
extern u8 keyboard_mod_buffer;
extern u16 cursor_x;
extern u16 cursor_y;
extern u16 mouse_buttons;
extern bool sd_needs_reinit;
u16 uS_Syscall(u16 u16_0, u16 u16_1, u16 u16_2, u16 u16_3, u8 syscall) {
switch (syscall) {
case SYS_BeginFrameDraw:
uS_BeginFrameDraw();
return 0;
case SYS_EndFrameDraw:
uS_EndFrameDraw();
return 0;
case SYS_WaitFrame:
uS_WaitFrame();
return 0;
case SYS_Blit: {
u8 bitmap[8];
u8 mask[8];
for (u8 i = 0; i < 8; i++) bitmap[i] = pgm_read_byte(u16_0 + i);
for (u8 i = 0; i < 8; i++) mask[i] = pgm_read_byte(u16_1 + i);
uS_Blit(bitmap, mask, u16_2, u16_3);
return 0;
}
case SYS_BlitRam:
uS_Blit((u8 *)u16_0, (u8 *)u16_1, u16_2, u16_3);
return 0;
case SYS_BlitChar:
uS_BlitChar((unsigned char)u16_0, u16_1, u16_2);
return 0;
case SYS_BlitStr:
uS_BlitStr((char *)u16_0, u16_1, u16_2);
return 0;
case SYS_BlitStrRam:
uS_BlitStrRam((char *)u16_0, u16_1, u16_2);
return 0;
case SYS_GetKey:
return (keyboard_mod_buffer << 8) | keyboard_buffer;
case SYS_GetMouseButtons:
return mouse_buttons;
case SYS_GetMouseX:
return cursor_x;
case SYS_GetMouseY:
return cursor_y;
case SYS_Open:
return uS_SDOpenFile((sd_file_t *)u16_0, (char *)u16_1, (u8)u16_2);
case SYS_Close:
return uS_SDCloseFile((sd_file_t *)u16_0);
case SYS_OpenDir:
return uS_SDOpenDir((sd_dir_t *)u16_0, (char *)u16_1);
case SYS_Read:
return uS_SDReadFile((sd_file_t *)u16_0, u16_1, u16_2, (u8 *)u16_3);
case SYS_ReadDir:
return uS_SDReadDir((sd_dir_t *)u16_0, (sd_file_info_t *)u16_1);
case SYS_Exec:
uS_Exec((char *)u16_0); // doesn't return on success
return -1;
case SYS_EnableVideo:
if (u16_0 != 0) {
m74_config |= M74_CFG_ENABLE;
sd_needs_reinit = true;
} else {
m74_config = 0;
}
return 0;
default:
return 0;
}
}
// install a `jmp uS_Syscall` trampoline opcode to 0xEF00
void uS_SyscallTrampolineInstall() {
memset(&page_buffer[0], 0, 256);
page_buffer[0] = 0x0C;
page_buffer[1] = 0x94;
page_buffer[2] = (u16)uS_Syscall & 0xFF;
page_buffer[3] = (u16)uS_Syscall >> 8;
uS_FlashPage(&page_buffer[0], 0xEF);
}