aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-05-20 21:45:04 +0200
committerBad Diode <bd@badd10de.dev>2021-05-20 21:45:04 +0200
commit1d6395f1b6aafce4e2e05bcf60f335bec0a8d4b3 (patch)
tree8e86d61333826dd2271ab7e997b1578d54f696ec /src/main.c
parent707b6dac805bcc4f2b5e297ff3ce4b82538bcff0 (diff)
downloaduxngba-1d6395f1b6aafce4e2e05bcf60f335bec0a8d4b3.tar.gz
uxngba-1d6395f1b6aafce4e2e05bcf60f335bec0a8d4b3.zip
Add datetime and control methods
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c99
1 files changed, 83 insertions, 16 deletions
diff --git a/src/main.c b/src/main.c
index e91a2f2..5edf71c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,5 @@
1#include <string.h> 1#include <string.h>
2#include <time.h>
2 3
3#include "common.h" 4#include "common.h"
4#include "bitmap.h" 5#include "bitmap.h"
@@ -23,11 +24,11 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
23WITH REGARD TO THIS SOFTWARE. 24WITH REGARD TO THIS SOFTWARE.
24*/ 25*/
25 26
26static size_t drawing_cycles = 0;
27
28static Ppu ppu; 27static Ppu ppu;
29u8 reqdraw = 0; 28u8 reqdraw = 0;
30static Device *devscreen; 29static Device *devscreen;
30static Device *devctrl;
31time_t time_seconds;
31 32
32void 33void
33nil_talk(Device *d, Uint8 b0, Uint8 w) { 34nil_talk(Device *d, Uint8 b0, Uint8 w) {
@@ -50,21 +51,18 @@ console_talk(Device *d, u8 b0, u8 w) {
50} 51}
51 52
52void 53void
53system_talk(Device *d, Uint8 b0, Uint8 w) 54system_talk(Device *d, Uint8 b0, Uint8 w) {
54{
55 if(!w) { 55 if(!w) {
56 d->dat[0x2] = d->u->wst.ptr; 56 d->dat[0x2] = d->u->wst.ptr;
57 d->dat[0x3] = d->u->rst.ptr; 57 d->dat[0x3] = d->u->rst.ptr;
58 } else { 58 } else {
59 putcolors(&ppu, &d->dat[0x8]); 59 putcolors(&ppu, &d->dat[0x8]);
60 reqdraw = 1;
61 } 60 }
62 (void)b0; 61 (void)b0;
63} 62}
64 63
65void 64void
66screen_talk(Device *d, Uint8 b0, Uint8 w) { 65screen_talk(Device *d, Uint8 b0, Uint8 w) {
67 // profile_start();
68 if(w && b0 == 0xe) { 66 if(w && b0 == 0xe) {
69 Uint16 x = mempeek16(d->dat, 0x8); 67 Uint16 x = mempeek16(d->dat, 0x8);
70 Uint16 y = mempeek16(d->dat, 0xa); 68 Uint16 y = mempeek16(d->dat, 0xa);
@@ -80,8 +78,23 @@ screen_talk(Device *d, Uint8 b0, Uint8 w) {
80 } 78 }
81 reqdraw = 1; 79 reqdraw = 1;
82 } 80 }
83 // int cycles = profile_stop(); 81}
84 // drawing_cycles += cycles; 82
83void
84datetime_talk(Device *d, Uint8 b0, Uint8 w) {
85 struct tm *t = localtime(&time_seconds);
86 t->tm_year += 1900;
87 mempoke16(d->dat, 0x0, t->tm_year);
88 d->dat[0x2] = t->tm_mon;
89 d->dat[0x3] = t->tm_mday;
90 d->dat[0x4] = t->tm_hour;
91 d->dat[0x5] = t->tm_min;
92 d->dat[0x6] = t->tm_sec;
93 d->dat[0x7] = t->tm_wday;
94 mempoke16(d->dat, 0x08, t->tm_yday);
95 d->dat[0xa] = t->tm_isdst;
96 (void)b0;
97 (void)w;
85} 98}
86 99
87void 100void
@@ -89,6 +102,9 @@ init_uxn(Uxn *u) {
89 // Initialize PPU. 102 // Initialize PPU.
90 initppu(&ppu, 30, 20, 0); 103 initppu(&ppu, 30, 20, 0);
91 104
105 // Init clock.
106 time_seconds = time(NULL);
107
92 // Copy rom to VM. 108 // Copy rom to VM.
93 memcpy(u->ram.dat + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom)); 109 memcpy(u->ram.dat + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom));
94 110
@@ -101,10 +117,10 @@ init_uxn(Uxn *u) {
101 portuxn(u, 0x5, "---", nil_talk); 117 portuxn(u, 0x5, "---", nil_talk);
102 portuxn(u, 0x6, "---", nil_talk); 118 portuxn(u, 0x6, "---", nil_talk);
103 portuxn(u, 0x7, "---", nil_talk); 119 portuxn(u, 0x7, "---", nil_talk);
104 portuxn(u, 0x8, "---", nil_talk); 120 devctrl = portuxn(u, 0x8, "controller", nil_talk);
105 portuxn(u, 0x9, "---", nil_talk); 121 portuxn(u, 0x9, "---", nil_talk);
106 portuxn(u, 0xa, "---", nil_talk); 122 portuxn(u, 0xa, "---", nil_talk);
107 portuxn(u, 0xb, "---", nil_talk); 123 portuxn(u, 0xb, "datetime", datetime_talk);
108 portuxn(u, 0xc, "---", nil_talk); 124 portuxn(u, 0xc, "---", nil_talk);
109 portuxn(u, 0xd, "---", nil_talk); 125 portuxn(u, 0xd, "---", nil_talk);
110 portuxn(u, 0xe, "---", nil_talk); 126 portuxn(u, 0xe, "---", nil_talk);
@@ -113,6 +129,58 @@ init_uxn(Uxn *u) {
113 mempoke16(devscreen->dat, 4, ppu.ver * 8); 129 mempoke16(devscreen->dat, 4, ppu.ver * 8);
114} 130}
115 131
132void
133handle_input(Uxn *u) {
134 poll_keys();
135
136 // TODO: We don't need ifs if we use KEY_INPUTS directly and mayvbe just
137 // swap some things if needed.
138 Uint8 *flag = &devctrl->dat[2];
139 if (key_pressed(KEY_A) | key_hold(KEY_A)) {
140 *flag |= 0x01;
141 } else {
142 *flag &= ~0x01;
143 }
144 if (key_pressed(KEY_B) | key_hold(KEY_B)) {
145 *flag |= 0x02;
146 } else {
147 *flag &= ~0x02;
148 }
149 if (key_pressed(KEY_L) | key_hold(KEY_L)) {
150 *flag |= 0x04;
151 } else {
152 *flag &= ~0x04;
153 }
154 if (key_pressed(KEY_R) | key_hold(KEY_R)) {
155 *flag |= 0x08;
156 } else {
157 *flag &= ~0x08;
158 }
159 if (key_pressed(KEY_UP) | key_hold(KEY_UP)) {
160 *flag |= 0x10;
161 } else {
162 *flag &= ~0x10;
163 }
164 if (key_pressed(KEY_DOWN) | key_hold(KEY_DOWN)) {
165 *flag |= 0x20;
166 } else {
167 *flag &= ~0x20;
168 }
169 if (key_pressed(KEY_LEFT) | key_hold(KEY_LEFT)) {
170 *flag |= 0x40;
171 } else {
172 *flag &= ~0x40;
173 }
174 if (key_pressed(KEY_RIGHT) | key_hold(KEY_RIGHT)) {
175 *flag |= 0x80;
176 } else {
177 *flag &= ~0x80;
178 }
179
180 evaluxn(u, mempeek16(devctrl->dat, 0));
181 devctrl->dat[3] = 0;
182}
183
116int main(void) { 184int main(void) {
117 // Register interrupts. 185 // Register interrupts.
118 irq_init(); 186 irq_init();
@@ -131,21 +199,20 @@ int main(void) {
131 .char_height = 8, 199 .char_height = 8,
132 .char_width = 8, 200 .char_width = 8,
133 }, &ppu.fg); 201 }, &ppu.fg);
202 txt_position(0,0);
134 203
135 // Main loop. 204 // Main loop.
136 int frame_counter = 0; 205 int frame_counter = 0;
137 evaluxn(&u, 0x0100); 206 evaluxn(&u, 0x0100);
207 // flipbuf(&ppu);
138 while(true) { 208 while(true) {
139 bios_vblank_wait(); 209 bios_vblank_wait();
140 poll_keys(); 210 handle_input(&u);
141 profile_start();
142 evaluxn(&u, mempeek16(devscreen->dat, 0)); 211 evaluxn(&u, mempeek16(devscreen->dat, 0));
143 flipbuf(&ppu); 212 flipbuf(&ppu);
144 int eval_cycles = profile_stop();
145 // txt_position(0,0); 213 // txt_position(0,0);
146 // txt_printf("FRAME: %d \n", frame_counter); 214 // txt_printf("FRAME: %d\n", frame_counter);
147 // txt_printf("EVAL: %d \n", eval_cycles); 215 time_seconds++;
148 drawing_cycles = 0;
149 frame_counter++; 216 frame_counter++;
150 } 217 }
151 218