Subversion Repositories Shiroi

Rev

Rev 11 | Rev 13 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 nishi 1
/* $Id: main.c 12 2024-08-29 04:19:08Z nishi $ */
2
 
3
#include <stdio.h>
4
#include <stdbool.h>
5
#include <sys/stat.h>
6
#include <stdlib.h>
7
#include <string.h>
8
 
9
#include <raylib.h>
10
 
11
#include "shiroi.h"
12
 
12 nishi 13
#define ON_COLOR ((Color){0xc0, 0, 0, 0xff})
14
#define OFF_COLOR ((Color){0x20, 0x20, 0x20, 0xff})
15
 
1 nishi 16
shiroi_t shiroi;
17
 
18
void thread_start(void);
19
void thread_end(void);
20
 
12 nishi 21
bool get_bit(int n, int f) { return (n & (1 << f)) ? true : false; }
22
 
1 nishi 23
int main(int argc, char** argv) {
24
	FILE* fconf = fopen("shiroi.ini", "r");
25
	if(fconf == NULL) {
26
		printf("Creating config.\n");
27
		fconf = fopen("shiroi.ini", "w");
28
		if(fconf == NULL) {
29
			fprintf(stderr, "Failed to create config\n");
30
			return 1;
31
		}
32
		fprintf(fconf, "slot1=video_mark_1\n");
33
		fprintf(fconf, "slot2=sound_mark_1\n");
34
		fprintf(fconf, "slot3=math_mark_1\n");
3 nishi 35
		fprintf(fconf, "slot4=text_mark_1\n");
1 nishi 36
		fprintf(fconf, "rom=shiroi.rom\n");
37
		fclose(fconf);
38
	}
39
 
40
	shiroi_init_cards(&shiroi);
41
 
42
	fconf = fopen("shiroi.ini", "r");
43
 
44
	struct stat s;
45
	stat("shiroi.ini", &s);
46
 
47
	char* ini = malloc(s.st_size + 1);
48
	fread(ini, s.st_size, 1, fconf);
49
 
50
	ini[s.st_size] = 0;
51
 
52
	fclose(fconf);
53
 
54
	int incr = 0;
55
	int i;
56
	for(i = 0;; i++) {
57
		if(ini[i] == 0 || ini[i] == '\n') {
58
			char oldc = ini[i];
59
			ini[i] = 0;
60
 
61
			char* line = ini + incr;
62
 
3 nishi 63
			if(strlen(line) != 0 && line[0] != '#') {
1 nishi 64
				int j;
65
				for(j = 0; line[j] != 0; j++) {
66
					if(line[j] == '=') {
67
						line[j] = 0;
68
						if(strcmp(line, "rom") == 0) {
69
							printf("ROM: %s\n", line + j + 1);
70
							if(stat(line + j + 1, &s) != 0) {
71
								fprintf(stderr, "ROM not found\n");
72
								free(ini);
73
								return 1;
74
							}
75
							printf("ROM size: %d\n", s.st_size);
76
							FILE* f = fopen(line + j + 1, "rb");
77
							fread(shiroi.ram, s.st_size, 1, f);
78
							fclose(f);
79
						} else if(line[0] == 's' && line[1] == 'l' && line[2] == 'o' && line[3] == 't') {
80
							int slot = atoi(line + 4);
81
							const char* n = "";
82
							int dev = -1;
83
							if(strcmp(line + j + 1, "video_mark_1") == 0) {
84
								dev = SHIROI_VIDEO_MARK_I;
85
								n = "Video Mark I";
7 nishi 86
							} else if(strcmp(line + j + 1, "video_mark_2") == 0) {
87
								dev = SHIROI_VIDEO_MARK_II;
88
								n = "Video Mark II";
1 nishi 89
							} else if(strcmp(line + j + 1, "sound_mark_1") == 0) {
90
								dev = SHIROI_SOUND_MARK_I;
91
								n = "Sound Mark I";
92
							} else if(strcmp(line + j + 1, "math_mark_1") == 0) {
93
								dev = SHIROI_MATH_MARK_I;
94
								n = "Math Mark I";
3 nishi 95
							} else if(strcmp(line + j + 1, "text_mark_1") == 0) {
96
								dev = SHIROI_TEXT_MARK_I;
97
								n = "Text Mark I";
12 nishi 98
							} else if(strcmp(line + j + 1, "debug") == 0) {
99
								dev = SHIROI_DEBUG;
100
								n = "Debug";
1 nishi 101
							}
102
							if(dev == -1) {
103
								fprintf(stderr, "No such device called `%s' ; ignoring\n", line + j + 1);
104
							} else {
105
								shiroi_install(&shiroi, slot, dev);
106
								printf("Installed `%s' into slot %d\n", n, slot);
107
							}
108
						}
109
						break;
110
					}
111
				}
112
			}
113
 
114
			incr = i + 1;
115
			if(oldc == 0) break;
116
		}
117
	}
118
 
119
	free(ini);
120
 
121
	shiroi_init(&shiroi);
122
 
123
	double scx = 2;
124
	double scy = 2;
125
 
126
	shiroi_card_t* videocard = shiroi_get_video_card(&shiroi);
127
	shiroi_video_t* video = NULL;
128
	if(videocard != NULL) {
129
		video = videocard->videoptr;
130
	}
12 nishi 131
 
3 nishi 132
	shiroi_card_t* textcard = shiroi_get_text_card(&shiroi);
133
	shiroi_text_t* text = NULL;
134
	if(textcard != NULL) {
135
		text = textcard->textptr;
136
	}
1 nishi 137
 
12 nishi 138
	shiroi_card_t* debugcard = shiroi_get_debug_card(&shiroi);
139
	shiroi_debug_t* debug = NULL;
140
	if(debugcard != NULL) {
141
		debug = debugcard->debugptr;
142
	}
143
 
1 nishi 144
	SetTraceLogLevel(LOG_NONE);
145
	if(video != NULL) {
12 nishi 146
		InitWindow(video->width * scx + (text == NULL ? 0 : 200), video->height * scy, "Shiroi Emulator");
1 nishi 147
	} else {
12 nishi 148
		InitWindow(640 + (text == NULL ? 0 : 200), 480, "Shiroi Emulator");
1 nishi 149
	}
150
	InitAudioDevice();
151
	SetAudioStreamBufferSizeDefault(512);
152
	AudioStream as = LoadAudioStream(48000, 16, 1);
153
	SetAudioStreamCallback(as, shiroi.play_audio);
154
	SetTargetFPS(60);
155
	uint32_t* fb = NULL;
156
 
157
	if(video != NULL) {
158
		fb = malloc(sizeof(*fb) * video->width * video->height);
159
 
160
		int x, y;
161
		for(y = 0; y < video->height; y++) {
162
			for(x = 0; x < video->width; x++) {
163
				fb[y * video->width + x] = 0xffffff;
164
			}
165
		}
166
	}
167
 
168
	RenderTexture r;
12 nishi 169
	if(video != NULL) {
170
		r = LoadRenderTexture(video->width, video->height);
171
		BeginTextureMode(r);
172
		ClearBackground(BLACK);
173
		EndTextureMode();
174
	}
1 nishi 175
	PlayAudioStream(as);
176
	thread_start();
177
	while(!WindowShouldClose()) {
178
		BeginDrawing();
179
 
180
		ClearBackground(BLACK);
181
 
3 nishi 182
		if(text != NULL) {
4 nishi 183
			/*
184
			 * / 1 2 3 4 5 6 7 8 9 10 11 12 13
7 nishi 185
			 * 1 1 2 3 4 5 6 7 8 9 0  -  =  bs
4 nishi 186
			 * 2 q w e r t y u i o p  [  ]  rt
187
			 * 3 a s d f g h j k l ;  '  \  cl
188
			 * 4 z x c v b n m , . /  sp
189
			 */
190
			int c = GetKeyPressed();
7 nishi 191
			if(KEY_ONE <= c && c <= KEY_NINE) {
4 nishi 192
				text->key = (1 << 4) | (c - KEY_ONE + 1);
5 nishi 193
			} else if(c == KEY_ZERO) {
4 nishi 194
				text->key = (1 << 4) | 10;
5 nishi 195
			} else if(c == KEY_MINUS) {
4 nishi 196
				text->key = (1 << 4) | 11;
5 nishi 197
			} else if(c == KEY_EQUAL) {
4 nishi 198
				text->key = (1 << 4) | 12;
7 nishi 199
			} else if(c == KEY_BACKSPACE) {
200
				text->key = (1 << 4) | 13;
5 nishi 201
			} else if(c == KEY_LEFT_BRACKET) {
4 nishi 202
				text->key = (2 << 4) | 11;
5 nishi 203
			} else if(c == KEY_RIGHT_BRACKET) {
4 nishi 204
				text->key = (2 << 4) | 12;
5 nishi 205
			} else if(c == KEY_ENTER) {
4 nishi 206
				text->key = (2 << 4) | 13;
5 nishi 207
			} else if(c == KEY_SEMICOLON) {
4 nishi 208
				text->key = (3 << 4) | 10;
5 nishi 209
			} else if(c == KEY_APOSTROPHE) {
4 nishi 210
				text->key = (3 << 4) | 11;
5 nishi 211
			} else if(c == KEY_BACKSLASH) {
4 nishi 212
				text->key = (3 << 4) | 12;
5 nishi 213
			} else if(c == KEY_LEFT_SHIFT || c == KEY_RIGHT_SHIFT) {
4 nishi 214
				text->key = (3 << 4) | 13;
6 nishi 215
				text->caps = !text->caps;
11 nishi 216
			} else if(c == KEY_F1) {
217
				if(shiroi.stop) {
218
					shiroi.stop = false;
219
					shiroi.reset = true;
220
					thread_start();
221
				} else {
222
					shiroi.reset = true;
223
					shiroi.stop = true;
224
					thread_end();
225
				}
10 nishi 226
			} else if(c == KEY_F2) {
227
				shiroi.reset = true;
5 nishi 228
			} else if(c == KEY_COMMA) {
4 nishi 229
				text->key = (4 << 4) | 8;
5 nishi 230
			} else if(c == KEY_PERIOD) {
4 nishi 231
				text->key = (4 << 4) | 9;
5 nishi 232
			} else if(c == KEY_SLASH) {
4 nishi 233
				text->key = (4 << 4) | 10;
5 nishi 234
			} else if(c == KEY_SPACE) {
4 nishi 235
				text->key = (4 << 4) | 11;
5 nishi 236
			} else if(c == KEY_Q) {
4 nishi 237
				text->key = (2 << 4) | 1;
5 nishi 238
			} else if(c == KEY_W) {
4 nishi 239
				text->key = (2 << 4) | 2;
5 nishi 240
			} else if(c == KEY_E) {
4 nishi 241
				text->key = (2 << 4) | 3;
5 nishi 242
			} else if(c == KEY_R) {
4 nishi 243
				text->key = (2 << 4) | 4;
5 nishi 244
			} else if(c == KEY_T) {
4 nishi 245
				text->key = (2 << 4) | 5;
5 nishi 246
			} else if(c == KEY_Y) {
4 nishi 247
				text->key = (2 << 4) | 6;
5 nishi 248
			} else if(c == KEY_U) {
4 nishi 249
				text->key = (2 << 4) | 7;
5 nishi 250
			} else if(c == KEY_I) {
4 nishi 251
				text->key = (2 << 4) | 8;
5 nishi 252
			} else if(c == KEY_O) {
4 nishi 253
				text->key = (2 << 4) | 9;
5 nishi 254
			} else if(c == KEY_P) {
4 nishi 255
				text->key = (2 << 4) | 10;
5 nishi 256
			} else if(c == KEY_A) {
4 nishi 257
				text->key = (3 << 4) | 1;
5 nishi 258
			} else if(c == KEY_S) {
4 nishi 259
				text->key = (3 << 4) | 2;
5 nishi 260
			} else if(c == KEY_D) {
4 nishi 261
				text->key = (3 << 4) | 3;
5 nishi 262
			} else if(c == KEY_F) {
4 nishi 263
				text->key = (3 << 4) | 4;
5 nishi 264
			} else if(c == KEY_G) {
4 nishi 265
				text->key = (3 << 4) | 5;
5 nishi 266
			} else if(c == KEY_H) {
4 nishi 267
				text->key = (3 << 4) | 6;
5 nishi 268
			} else if(c == KEY_J) {
4 nishi 269
				text->key = (3 << 4) | 7;
5 nishi 270
			} else if(c == KEY_K) {
4 nishi 271
				text->key = (3 << 4) | 8;
5 nishi 272
			} else if(c == KEY_L) {
4 nishi 273
				text->key = (3 << 4) | 9;
5 nishi 274
			} else if(c == KEY_Z) {
4 nishi 275
				text->key = (4 << 4) | 1;
5 nishi 276
			} else if(c == KEY_X) {
4 nishi 277
				text->key = (4 << 4) | 2;
5 nishi 278
			} else if(c == KEY_C) {
4 nishi 279
				text->key = (4 << 4) | 3;
5 nishi 280
			} else if(c == KEY_V) {
4 nishi 281
				text->key = (4 << 4) | 4;
5 nishi 282
			} else if(c == KEY_B) {
4 nishi 283
				text->key = (4 << 4) | 5;
5 nishi 284
			} else if(c == KEY_N) {
4 nishi 285
				text->key = (4 << 4) | 6;
5 nishi 286
			} else if(c == KEY_M) {
4 nishi 287
				text->key = (4 << 4) | 7;
288
			}
3 nishi 289
		}
290
 
1 nishi 291
		if(video != NULL) {
292
			BeginTextureMode(r);
293
 
294
			int y, x;
295
			for(y = 0; y < video->height; y++) {
296
				for(x = 0; x < video->width; x++) {
7 nishi 297
					if(video->fb[y * video->width + x] != fb[y * video->width + x]) {
1 nishi 298
						uint32_t c = video->fb[y * video->width + x];
7 nishi 299
						DrawPixel(x, y, (Color){(c >> 24) & 0xff, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff});
1 nishi 300
						fb[y * video->width + x] = c;
301
					}
302
				}
303
			}
304
 
305
			EndTextureMode();
306
 
12 nishi 307
			DrawTexturePro(r.texture, (Rectangle){0, 0, video->width, -video->height}, (Rectangle){text == NULL ? 0 : 200, 0, GetScreenWidth() - (text == NULL ? 0 : 200), GetScreenHeight()}, (Vector2){0, 0}, 0, WHITE);
1 nishi 308
		} else {
7 nishi 309
			DrawText("No Video", 0, 20, 20, WHITE);
1 nishi 310
		}
311
 
6 nishi 312
		DrawText("Caps Lock", 5, 5, 10, WHITE);
313
 
12 nishi 314
		DrawCircle(200 - 10, 10, 5, text->caps ? ON_COLOR : OFF_COLOR);
6 nishi 315
 
12 nishi 316
		if(debug != NULL) {
317
			DrawText("Debug", 5, 5 + 10 + 5, 10, WHITE);
318
			for(i = 0; i < 4; i++) {
319
				int shx = i * (5 + 20 + 5 + 5 + 10);
320
				DrawRectangle(shx + 10, 5 + 5 + 10 + 5 + 10 + 5, 20, 5, get_bit(debug->latch[i], 0) ? ON_COLOR : OFF_COLOR);
321
				DrawRectangle(shx + 5, 5 + 5 + 10 + 5 + 10 + 10, 5, 20, get_bit(debug->latch[i], 1) ? ON_COLOR : OFF_COLOR);
322
				DrawRectangle(shx + 5 + 20 + 5, 5 + 5 + 10 + 5 + 10 + 10, 5, 20, get_bit(debug->latch[i], 2) ? ON_COLOR : OFF_COLOR);
323
				DrawRectangle(shx + 10, 5 + 5 + 10 + 5 + 10 + 5 + 20 + 5, 20, 5, get_bit(debug->latch[i], 3) ? ON_COLOR : OFF_COLOR);
324
				DrawRectangle(shx + 5, 5 + 5 + 10 + 5 + 10 + 10 + 5 + 20, 5, 20, get_bit(debug->latch[i], 4) ? ON_COLOR : OFF_COLOR);
325
				DrawRectangle(shx + 5 + 20 + 5, 5 + 5 + 10 + 5 + 10 + 10 + 5 + 20, 5, 20, get_bit(debug->latch[i], 5) ? ON_COLOR : OFF_COLOR);
326
				DrawRectangle(shx + 10, 5 + 5 + 10 + 5 + 10 + 5 + 20 + 5 + 20 + 5, 20, 5, get_bit(debug->latch[i], 6) ? ON_COLOR : OFF_COLOR);
327
				DrawRectangle(shx + 5 + 5 + 20 + 5 + 5, 5 + 5 + 10 + 5 + 10 + 5 + 20 + 5 + 20 + 5, 5, 5, get_bit(debug->latch[i], 7) ? ON_COLOR : OFF_COLOR);
328
			}
329
		}
330
 
1 nishi 331
		EndDrawing();
332
	}
333
	CloseWindow();
334
	UnloadAudioStream(as);
335
	if(video != NULL) UnloadRenderTexture(r);
336
	shiroi.stop = true;
337
	thread_end();
338
}