Subversion Repositories Shiroi

Rev

Rev 34 | 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 41 2024-09-02 03:56:45Z 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);
34 nishi 79
						} else if(strcmp(line, "romcard") == 0) {
80
							printf("ROMcard: %s\n", line + j + 1);
81
							if(stat(line + j + 1, &s) != 0) {
82
								fprintf(stderr, "ROM not found\n");
83
								free(ini);
84
								return 1;
85
							}
86
							printf("ROMcard size: %d\n", s.st_size);
87
							shiroi_card_t* card = shiroi_get_romcard_card(&shiroi);
88
							if(card != NULL) {
89
								FILE* f = fopen(line + j + 1, "rb");
90
								fread(card->romcard.data, s.st_size, 1, f);
91
								fclose(f);
92
							} else {
93
								fprintf(stderr, "ROMcard is not installed ; ignored\n");
94
							}
1 nishi 95
						} else if(line[0] == 's' && line[1] == 'l' && line[2] == 'o' && line[3] == 't') {
96
							int slot = atoi(line + 4);
97
							const char* n = "";
98
							int dev = -1;
99
							if(strcmp(line + j + 1, "video_mark_1") == 0) {
100
								dev = SHIROI_VIDEO_MARK_I;
101
								n = "Video Mark I";
7 nishi 102
							} else if(strcmp(line + j + 1, "video_mark_2") == 0) {
103
								dev = SHIROI_VIDEO_MARK_II;
104
								n = "Video Mark II";
1 nishi 105
							} else if(strcmp(line + j + 1, "sound_mark_1") == 0) {
106
								dev = SHIROI_SOUND_MARK_I;
107
								n = "Sound Mark I";
108
							} else if(strcmp(line + j + 1, "math_mark_1") == 0) {
109
								dev = SHIROI_MATH_MARK_I;
110
								n = "Math Mark I";
3 nishi 111
							} else if(strcmp(line + j + 1, "text_mark_1") == 0) {
112
								dev = SHIROI_TEXT_MARK_I;
113
								n = "Text Mark I";
12 nishi 114
							} else if(strcmp(line + j + 1, "debug") == 0) {
115
								dev = SHIROI_DEBUG;
116
								n = "Debug";
34 nishi 117
							} else if(strcmp(line + j + 1, "romcard_mark_1") == 0) {
118
								dev = SHIROI_ROMCARD_MARK_I;
119
								n = "ROMcard Mark I";
1 nishi 120
							}
121
							if(dev == -1) {
122
								fprintf(stderr, "No such device called `%s' ; ignoring\n", line + j + 1);
123
							} else {
124
								shiroi_install(&shiroi, slot, dev);
125
								printf("Installed `%s' into slot %d\n", n, slot);
126
							}
127
						}
128
						break;
129
					}
130
				}
131
			}
132
 
133
			incr = i + 1;
134
			if(oldc == 0) break;
135
		}
136
	}
137
 
138
	free(ini);
139
 
140
	shiroi_init(&shiroi);
141
 
142
	double scx = 2;
143
	double scy = 2;
144
 
145
	shiroi_card_t* videocard = shiroi_get_video_card(&shiroi);
146
	shiroi_video_t* video = NULL;
147
	if(videocard != NULL) {
148
		video = videocard->videoptr;
149
	}
12 nishi 150
 
3 nishi 151
	shiroi_card_t* textcard = shiroi_get_text_card(&shiroi);
152
	shiroi_text_t* text = NULL;
153
	if(textcard != NULL) {
154
		text = textcard->textptr;
155
	}
1 nishi 156
 
12 nishi 157
	shiroi_card_t* debugcard = shiroi_get_debug_card(&shiroi);
158
	shiroi_debug_t* debug = NULL;
159
	if(debugcard != NULL) {
160
		debug = debugcard->debugptr;
161
	}
162
 
1 nishi 163
	SetTraceLogLevel(LOG_NONE);
164
	if(video != NULL) {
13 nishi 165
		InitWindow(video->width * scx + ((debug == NULL && text == NULL) ? 0 : 200), video->height * scy, "Shiroi Emulator");
1 nishi 166
	} else {
13 nishi 167
		InitWindow(640 + ((debug == NULL && text == NULL) ? 0 : 200), 480, "Shiroi Emulator");
1 nishi 168
	}
169
	InitAudioDevice();
170
	SetAudioStreamBufferSizeDefault(512);
171
	AudioStream as = LoadAudioStream(48000, 16, 1);
172
	SetAudioStreamCallback(as, shiroi.play_audio);
173
	SetTargetFPS(60);
174
	uint32_t* fb = NULL;
175
 
176
	if(video != NULL) {
177
		fb = malloc(sizeof(*fb) * video->width * video->height);
178
 
179
		int x, y;
180
		for(y = 0; y < video->height; y++) {
181
			for(x = 0; x < video->width; x++) {
182
				fb[y * video->width + x] = 0xffffff;
183
			}
184
		}
185
	}
186
 
187
	RenderTexture r;
12 nishi 188
	if(video != NULL) {
189
		r = LoadRenderTexture(video->width, video->height);
190
		BeginTextureMode(r);
191
		ClearBackground(BLACK);
192
		EndTextureMode();
193
	}
1 nishi 194
	PlayAudioStream(as);
195
	thread_start();
196
	while(!WindowShouldClose()) {
197
		BeginDrawing();
198
 
199
		ClearBackground(BLACK);
200
 
3 nishi 201
		if(text != NULL) {
4 nishi 202
			/*
203
			 * / 1 2 3 4 5 6 7 8 9 10 11 12 13
7 nishi 204
			 * 1 1 2 3 4 5 6 7 8 9 0  -  =  bs
4 nishi 205
			 * 2 q w e r t y u i o p  [  ]  rt
206
			 * 3 a s d f g h j k l ;  '  \  cl
20 nishi 207
			 * 4 z x c v b n m , . /  sp bk
4 nishi 208
			 */
209
			int c = GetKeyPressed();
7 nishi 210
			if(KEY_ONE <= c && c <= KEY_NINE) {
4 nishi 211
				text->key = (1 << 4) | (c - KEY_ONE + 1);
5 nishi 212
			} else if(c == KEY_ZERO) {
4 nishi 213
				text->key = (1 << 4) | 10;
5 nishi 214
			} else if(c == KEY_MINUS) {
4 nishi 215
				text->key = (1 << 4) | 11;
5 nishi 216
			} else if(c == KEY_EQUAL) {
4 nishi 217
				text->key = (1 << 4) | 12;
7 nishi 218
			} else if(c == KEY_BACKSPACE) {
219
				text->key = (1 << 4) | 13;
5 nishi 220
			} else if(c == KEY_LEFT_BRACKET) {
4 nishi 221
				text->key = (2 << 4) | 11;
5 nishi 222
			} else if(c == KEY_RIGHT_BRACKET) {
4 nishi 223
				text->key = (2 << 4) | 12;
5 nishi 224
			} else if(c == KEY_ENTER) {
4 nishi 225
				text->key = (2 << 4) | 13;
5 nishi 226
			} else if(c == KEY_SEMICOLON) {
4 nishi 227
				text->key = (3 << 4) | 10;
5 nishi 228
			} else if(c == KEY_APOSTROPHE) {
4 nishi 229
				text->key = (3 << 4) | 11;
5 nishi 230
			} else if(c == KEY_BACKSLASH) {
4 nishi 231
				text->key = (3 << 4) | 12;
5 nishi 232
			} else if(c == KEY_LEFT_SHIFT || c == KEY_RIGHT_SHIFT) {
4 nishi 233
				text->key = (3 << 4) | 13;
6 nishi 234
				text->caps = !text->caps;
11 nishi 235
			} else if(c == KEY_F1) {
236
				if(shiroi.stop) {
237
					shiroi.stop = false;
238
					shiroi.reset = true;
239
					thread_start();
240
				} else {
241
					shiroi.reset = true;
242
					shiroi.stop = true;
243
					thread_end();
244
				}
10 nishi 245
			} else if(c == KEY_F2) {
246
				shiroi.reset = true;
5 nishi 247
			} else if(c == KEY_COMMA) {
4 nishi 248
				text->key = (4 << 4) | 8;
5 nishi 249
			} else if(c == KEY_PERIOD) {
4 nishi 250
				text->key = (4 << 4) | 9;
5 nishi 251
			} else if(c == KEY_SLASH) {
4 nishi 252
				text->key = (4 << 4) | 10;
5 nishi 253
			} else if(c == KEY_SPACE) {
4 nishi 254
				text->key = (4 << 4) | 11;
20 nishi 255
			} else if((IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL)) && c == KEY_PAUSE) {
256
				text->key = (4 << 4) | 12;
5 nishi 257
			} else if(c == KEY_Q) {
4 nishi 258
				text->key = (2 << 4) | 1;
5 nishi 259
			} else if(c == KEY_W) {
4 nishi 260
				text->key = (2 << 4) | 2;
5 nishi 261
			} else if(c == KEY_E) {
4 nishi 262
				text->key = (2 << 4) | 3;
5 nishi 263
			} else if(c == KEY_R) {
4 nishi 264
				text->key = (2 << 4) | 4;
5 nishi 265
			} else if(c == KEY_T) {
4 nishi 266
				text->key = (2 << 4) | 5;
5 nishi 267
			} else if(c == KEY_Y) {
4 nishi 268
				text->key = (2 << 4) | 6;
5 nishi 269
			} else if(c == KEY_U) {
4 nishi 270
				text->key = (2 << 4) | 7;
5 nishi 271
			} else if(c == KEY_I) {
4 nishi 272
				text->key = (2 << 4) | 8;
5 nishi 273
			} else if(c == KEY_O) {
4 nishi 274
				text->key = (2 << 4) | 9;
5 nishi 275
			} else if(c == KEY_P) {
4 nishi 276
				text->key = (2 << 4) | 10;
5 nishi 277
			} else if(c == KEY_A) {
4 nishi 278
				text->key = (3 << 4) | 1;
5 nishi 279
			} else if(c == KEY_S) {
4 nishi 280
				text->key = (3 << 4) | 2;
5 nishi 281
			} else if(c == KEY_D) {
4 nishi 282
				text->key = (3 << 4) | 3;
5 nishi 283
			} else if(c == KEY_F) {
4 nishi 284
				text->key = (3 << 4) | 4;
5 nishi 285
			} else if(c == KEY_G) {
4 nishi 286
				text->key = (3 << 4) | 5;
5 nishi 287
			} else if(c == KEY_H) {
4 nishi 288
				text->key = (3 << 4) | 6;
5 nishi 289
			} else if(c == KEY_J) {
4 nishi 290
				text->key = (3 << 4) | 7;
5 nishi 291
			} else if(c == KEY_K) {
4 nishi 292
				text->key = (3 << 4) | 8;
5 nishi 293
			} else if(c == KEY_L) {
4 nishi 294
				text->key = (3 << 4) | 9;
5 nishi 295
			} else if(c == KEY_Z) {
4 nishi 296
				text->key = (4 << 4) | 1;
5 nishi 297
			} else if(c == KEY_X) {
4 nishi 298
				text->key = (4 << 4) | 2;
5 nishi 299
			} else if(c == KEY_C) {
4 nishi 300
				text->key = (4 << 4) | 3;
5 nishi 301
			} else if(c == KEY_V) {
4 nishi 302
				text->key = (4 << 4) | 4;
5 nishi 303
			} else if(c == KEY_B) {
4 nishi 304
				text->key = (4 << 4) | 5;
5 nishi 305
			} else if(c == KEY_N) {
4 nishi 306
				text->key = (4 << 4) | 6;
5 nishi 307
			} else if(c == KEY_M) {
4 nishi 308
				text->key = (4 << 4) | 7;
309
			}
3 nishi 310
		}
311
 
1 nishi 312
		if(video != NULL) {
313
			BeginTextureMode(r);
314
 
315
			int y, x;
316
			for(y = 0; y < video->height; y++) {
317
				for(x = 0; x < video->width; x++) {
7 nishi 318
					if(video->fb[y * video->width + x] != fb[y * video->width + x]) {
1 nishi 319
						uint32_t c = video->fb[y * video->width + x];
7 nishi 320
						DrawPixel(x, y, (Color){(c >> 24) & 0xff, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff});
1 nishi 321
						fb[y * video->width + x] = c;
322
					}
323
				}
324
			}
325
 
326
			EndTextureMode();
327
 
13 nishi 328
			DrawTexturePro(r.texture, (Rectangle){0, 0, video->width, -video->height}, (Rectangle){(text == NULL && debug == NULL) ? 0 : 200, 0, GetScreenWidth() - ((text == NULL && debug == NULL) ? 0 : 200), GetScreenHeight()}, (Vector2){0, 0}, 0, WHITE);
1 nishi 329
		} else {
13 nishi 330
			DrawText("No Video", 200 + 5, 5, 20, WHITE);
1 nishi 331
		}
332
 
13 nishi 333
		if(text != NULL) {
334
			DrawText("Caps Lock", 5, 5, 10, WHITE);
6 nishi 335
 
13 nishi 336
			DrawCircle(200 - 10, 10, 5, text->caps ? ON_COLOR : OFF_COLOR);
337
		}
6 nishi 338
 
12 nishi 339
		if(debug != NULL) {
340
			DrawText("Debug", 5, 5 + 10 + 5, 10, WHITE);
341
			for(i = 0; i < 4; i++) {
342
				int shx = i * (5 + 20 + 5 + 5 + 10);
343
				DrawRectangle(shx + 10, 5 + 5 + 10 + 5 + 10 + 5, 20, 5, get_bit(debug->latch[i], 0) ? ON_COLOR : OFF_COLOR);
344
				DrawRectangle(shx + 5, 5 + 5 + 10 + 5 + 10 + 10, 5, 20, get_bit(debug->latch[i], 1) ? ON_COLOR : OFF_COLOR);
345
				DrawRectangle(shx + 5 + 20 + 5, 5 + 5 + 10 + 5 + 10 + 10, 5, 20, get_bit(debug->latch[i], 2) ? ON_COLOR : OFF_COLOR);
346
				DrawRectangle(shx + 10, 5 + 5 + 10 + 5 + 10 + 5 + 20 + 5, 20, 5, get_bit(debug->latch[i], 3) ? ON_COLOR : OFF_COLOR);
347
				DrawRectangle(shx + 5, 5 + 5 + 10 + 5 + 10 + 10 + 5 + 20, 5, 20, get_bit(debug->latch[i], 4) ? ON_COLOR : OFF_COLOR);
348
				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);
349
				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);
350
				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);
351
			}
352
		}
353
 
1 nishi 354
		EndDrawing();
355
	}
41 nishi 356
	if(video != NULL) UnloadRenderTexture(r);
1 nishi 357
	CloseWindow();
358
	UnloadAudioStream(as);
359
	shiroi.stop = true;
360
	thread_end();
361
}