• R/O
  • HTTP
  • SSH
  • HTTPS

HeavyOSECPU: Commit


Commit MetaInfo

Revisióncda6c2b0bc9570e7c1efe0fd801e2ee04665cb13 (tree)
Tiempo2014-03-13 23:23:01
Autorhikarupsp <hikarupsp@user...>
Commiterhikarupsp

Log Message

junkApi_exitのリターンコードを反映するようにした。

Cambiar Resumen

Diferencia incremental

--- a/dpndenv.c
+++ b/dpndenv.c
@@ -1,428 +1,430 @@
1-#include "osecpu.h"
2-
3-
4-#if (DRV_OSNUM == 0x0002)
5-//
6-// for Mac OSX 32-bit
7-//
8-#include <mach/mach.h>
9-#include <Cocoa/Cocoa.h>
10-
11-void *mallocRWE(int bytes)
12-{
13- void *p = malloc(bytes);
14- vm_protect(mach_task_self(), (vm_address_t) p, bytes, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
15- return p;
16-}
17-
18-NSApplication* app;
19-
20-@interface OSECPUView : NSView
21-{
22- unsigned char *_buf;
23- int _sx;
24- int _sy;
25- CGContextRef _context;
26-}
27-
28-- (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy;
29-- (void)drawRect:(NSRect)rect;
30-@end
31-
32-@implementation OSECPUView
33-- (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy
34-{
35- self = [super initWithFrame:frameRect];
36- if (self) {
37- _buf = buf;
38- _sx = sx;
39- _sy = sy;
40- }
41- return self;
42-}
43-
44-- (void)drawRect:(NSRect)rect {
45- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
46- _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));
47- CGImageRef image = CGBitmapContextCreateImage(_context);
48- CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
49- CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);
50-
51- CFRelease(colorSpace);
52- CFRelease(image);
53-}
54-
55-@end
56-
57-@interface Main : NSObject<NSWindowDelegate>
58-{
59- int argc;
60- const unsigned char **argv;
61- char *winClosed;
62- OSECPUView *_view;
63-}
64-
65-- (void)runApp;
66-- (void)createThread:(int)_argc args:(const unsigned char **)_argv;
67-- (BOOL)windowShouldClose:(id)sender;
68-- (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int)sy winClosed:(char *)_winClosed;
69-- (void)flushWin:(NSRect)rect;
70-@end
71-
72-@implementation Main
73-- (void)runApp
74-{
75- NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
76- HeavyOSECPUMain(argc, (char **)argv);
77- [NSApp terminate:self];
78- [pool release];
79-}
80-
81-- (void)createThread : (int)_argc args:(const unsigned char **)_argv
82-{
83- argc = _argc;
84- argv = _argv;
85- NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
86- [thread start];
87-}
88-
89-- (BOOL)windowShouldClose:(id)sender
90-{
91- *winClosed = 1;
92- return YES;
93-}
94-
95-- (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int) sy winClosed:(char *)_winClosed
96-{
97-
98- NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
99- [window setTitle: @"osecpu"];
100- [window center];
101- [window makeKeyAndOrderFront:nil];
102- [window setReleasedWhenClosed:YES];
103- window.delegate = self;
104- winClosed = _winClosed;
105-
106- _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
107- [window.contentView addSubview:_view];
108-}
109-
110-- (void)flushWin : (NSRect)rect
111-{
112- [_view drawRect:rect];
113-}
114-
115-@end
116-
117-id objc_main;
118-
119-int main(int argc, char **argv)
120-{
121- objc_main = [[Main alloc] init];
122-
123- NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
124- app = [[NSApplication alloc] init];
125- [objc_main createThread:argc args:(const unsigned char **)argv];
126- [app run];
127- [pool release];
128- return 0;
129-}
130-
131-void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
132-{
133- [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
134-}
135-
136-void drv_flshWin(int sx, int sy, int x0, int y0)
137-{
138- [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
139-}
140-
141-void drv_sleep(int msec)
142-{
143- [NSThread sleepForTimeInterval:0.001*msec];
144- return;
145-}
146-
147-
148-#elif (DRV_OSNUM == 0x0003)
149-
150-#error "Your OS is not supported."
151-
152-#elif (DRV_OSNUM == 0x0001)
153-//
154-// for Windows 32-bit
155-//
156-#include <windows.h>
157-
158-#define TIMER_ID 1
159-#define TIMER_INTERVAL 10
160-
161-struct BLD_WORK {
162- HINSTANCE hi;
163- HWND hw;
164- BITMAPINFO bmi;
165- int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
166- HANDLE mtx;
167- char *winClosed;
168-};
169-
170-struct BLD_WORK bld_work;
171-
172-struct BL_WIN {
173- int xsiz, ysiz, *buf;
174-};
175-
176-struct BL_WORK {
177- struct BL_WIN win;
178- jmp_buf jb;
179- int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
180- int tmcount, tmcount0, mod, rand_seed;
181- int *cbuf;
182- unsigned char *ftyp;
183- unsigned char **fptn;
184- int *ccol, *cbak;
185- int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
186-};
187-
188-struct BL_WORK bl_work;
189-
190-#define BL_SIZ_KBUF 8192
191-
192-#define BL_WAITKEYF 0x00000001
193-#define BL_WAITKEYNF 0x00000002
194-#define BL_WAITKEY 0x00000003
195-#define BL_GETKEY 0x00000004
196-#define BL_CLEARREP 0x00000008
197-#define BL_DELFFF 0x00000010
198-
199-#define BL_KEYMODE 0x00000000 // 作りかけ, make/remake/breakが見えるかどうか
200-
201-#define w bl_work
202-#define dw bld_work
203-
204-void bld_openWin(int x, int y, char *winClosed);
205-void bld_flshWin(int sx, int sy, int x0, int y0);
206-LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
207-void bl_cls();
208-int bl_iCol(int i);
209-void bl_readyWin(int n);
210-
211-static HANDLE threadhandle;
212-
213-int main(int argc, char **argv)
214-{
215- // Program entry point
216- return HeavyOSECPUMain(argc, argv);
217-}
218-
219-void *mallocRWE(int bytes)
220-{
221- void *p = malloc(bytes);
222- DWORD dmy;
223- VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
224- return p;
225-}
226-
227-static int winthread(void *dmy)
228-{
229- WNDCLASSEX wc;
230- RECT r;
231- int i, x, y;
232- MSG msg;
233-
234- x = dw.bmi.bmiHeader.biWidth;
235- y = -dw.bmi.bmiHeader.biHeight;
236-
237- wc.cbSize = sizeof (WNDCLASSEX);
238- wc.style = CS_HREDRAW | CS_VREDRAW;
239- wc.lpfnWndProc = WndProc;
240- wc.cbClsExtra = 0;
241- wc.cbWndExtra = 0;
242- wc.hInstance = dw.hi;
243- wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
244- IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
245- wc.hIconSm = wc.hIcon;
246- wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
247- IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
248- wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
249- wc.lpszMenuName = NULL;
250- wc.lpszClassName = "WinClass";
251- if (RegisterClassEx(&wc) == 0)
252- return 1;
253- r.left = 0;
254- r.top = 0;
255- r.right = x;
256- r.bottom = y;
257- AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
258- x = r.right - r.left;
259- y = r.bottom - r.top;
260-
261- char *t = "osecpu";
262-
263- dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
264- CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
265- if (dw.hw == NULL)
266- return 1;
267- ShowWindow(dw.hw, SW_SHOW);
268- UpdateWindow(dw.hw);
269- SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
270- SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
271- SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
272- dw.flags |= 2 | 4;
273-
274- for (;;) {
275- i = GetMessage(&msg, NULL, 0, 0);
276- if (i == 0 || i == -1) /* エラーもしくは終了メッセージ */
277- break;
278- /* そのほかはとりあえずデフォルト処理で */
279- TranslateMessage(&msg);
280- DispatchMessage(&msg);
281- }
282- // PostQuitMessage(0);
283- dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
284- if (dw.winClosed != NULL)
285- *dw.winClosed = 1;
286- return 0;
287-}
288-
289-void bld_openWin(int sx, int sy, char *winClosed)
290-{
291- static int i;
292-
293- dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
294- dw.bmi.bmiHeader.biWidth = sx;
295- dw.bmi.bmiHeader.biHeight = -sy;
296- dw.bmi.bmiHeader.biPlanes = 1;
297- dw.bmi.bmiHeader.biBitCount = 32;
298- dw.bmi.bmiHeader.biCompression = BI_RGB;
299- dw.winClosed = winClosed;
300-
301- threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
302-
303- return;
304-}
305-
306-void drv_flshWin(int sx, int sy, int x0, int y0)
307-{
308- InvalidateRect(dw.hw, NULL, FALSE);
309- UpdateWindow(dw.hw);
310- return;
311-}
312-
313-LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
314-{
315- int i, j;
316- if (msg == WM_PAINT) {
317- PAINTSTRUCT ps;
318- HDC hdc = BeginPaint(dw.hw, &ps);
319- SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
320- 0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
321- EndPaint(dw.hw, &ps);
322- }
323- if (msg == WM_DESTROY) {
324- PostQuitMessage(0);
325- return 0;
326- }
327- if (msg == WM_TIMER && wp == TIMER_ID) {
328- w.tmcount += TIMER_INTERVAL;
329- return 0;
330- }
331- if (msg == WM_TIMER && wp == TIMER_ID + 1) {
332- dw.tmcount1 += TIMER_INTERVAL * 10;
333- w.tmcount = dw.tmcount1;
334- return 0;
335- }
336- if (msg == WM_TIMER && wp == TIMER_ID + 2) {
337- dw.tmcount2 += TIMER_INTERVAL * 100;
338- w.tmcount = dw.tmcount1 = dw.tmcount2;
339- return 0;
340- }
341- if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
342- i = -1;
343-
344- if (wp == VK_RETURN) i = KEY_ENTER;
345- if (wp == VK_ESCAPE) i = KEY_ESC;
346- if (wp == VK_BACK) i = KEY_BACKSPACE;
347- if (wp == VK_TAB) i = KEY_TAB;
348- if (wp == VK_PRIOR) i = KEY_PAGEUP;
349- if (wp == VK_NEXT) i = KEY_PAGEDWN;
350- if (wp == VK_END) i = KEY_END;
351- if (wp == VK_HOME) i = KEY_HOME;
352- if (wp == VK_LEFT) i = KEY_LEFT;
353- if (wp == VK_RIGHT) i = KEY_RIGHT;
354- if (wp == VK_UP) i = KEY_UP;
355- if (wp == VK_DOWN) i = KEY_DOWN;
356- if (wp == VK_INSERT) i = KEY_INS;
357- if (wp == VK_DELETE) i = KEY_DEL;
358- j &= 0;
359- if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
360- if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
361- if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
362- if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
363- if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
364- if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
365- if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
366- if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
367- if (j != 0) {
368- if ('A' <= wp && wp <= 'Z') i = wp;
369- }
370- if (i != -1) {
371- putKeybuf(i | j);
372- // bl_putKeyB(1, &i);
373- return 0;
374- }
375- }
376- if (msg == WM_KEYUP) {
377- i = 0xfff;
378- // bl_putKeyB(1, &i);
379- }
380- if (msg == WM_CHAR) {
381- i = 0;
382- if (' ' <= wp && wp <= 0x7e) {
383- i = wp;
384- j &= 0;
385- if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
386- if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
387- if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
388- if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
389- if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
390- if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
391- if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
392- if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
393- if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
394- if (j != 0) {
395- i |= j;
396- i &= ~0x20;
397- }
398- }
399- putKeybuf(i);
400- // bl_putKeyB(1, &i);
401- return 0;
402- }
403- }
404- return DefWindowProc(hw, msg, wp, lp);
405-}
406-
407-void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
408-{
409- w.win.buf = (int *)buf;
410- w.win.xsiz = sx;
411- w.win.ysiz = sy;
412- bld_openWin(sx, sy, winClosed);
413- return;
414-}
415-
416-void drv_sleep(int msec)
417-{
418- Sleep(msec);
419- // MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
420- /* 勉強不足でまだ書き方が分かりません! */
421- return;
422-}
423-
424-#else
425-
426-#error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"
427-
1+#include "osecpu.h"
2+
3+
4+#if (DRV_OSNUM == 0x0002)
5+//
6+// for Mac OSX 32-bit
7+//
8+#include <mach/mach.h>
9+#include <Cocoa/Cocoa.h>
10+
11+void *mallocRWE(int bytes)
12+{
13+ void *p = malloc(bytes);
14+ vm_protect(mach_task_self(), (vm_address_t) p, bytes, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
15+ return p;
16+}
17+
18+NSApplication* app;
19+
20+@interface OSECPUView : NSView
21+{
22+ unsigned char *_buf;
23+ int _sx;
24+ int _sy;
25+ CGContextRef _context;
26+}
27+
28+- (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy;
29+- (void)drawRect:(NSRect)rect;
30+@end
31+
32+@implementation OSECPUView
33+- (id)initWithFrame:(NSRect)frameRect buf:(unsigned char *)buf sx:(int)sx sy:(int)sy
34+{
35+ self = [super initWithFrame:frameRect];
36+ if (self) {
37+ _buf = buf;
38+ _sx = sx;
39+ _sy = sy;
40+ }
41+ return self;
42+}
43+
44+- (void)drawRect:(NSRect)rect {
45+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
46+ _context = CGBitmapContextCreate (_buf, _sx, _sy, 8, 4 * _sx, colorSpace, (kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst));
47+ CGImageRef image = CGBitmapContextCreateImage(_context);
48+ CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
49+ CGContextDrawImage(currentContext, NSRectToCGRect(rect), image);
50+
51+ CFRelease(colorSpace);
52+ CFRelease(image);
53+}
54+
55+@end
56+
57+@interface Main : NSObject<NSWindowDelegate>
58+{
59+ int argc;
60+ const unsigned char **argv;
61+ char *winClosed;
62+ OSECPUView *_view;
63+}
64+
65+- (void)runApp;
66+- (void)createThread:(int)_argc args:(const unsigned char **)_argv;
67+- (BOOL)windowShouldClose:(id)sender;
68+- (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int)sy winClosed:(char *)_winClosed;
69+- (void)flushWin:(NSRect)rect;
70+@end
71+
72+@implementation Main
73+- (void)runApp
74+{
75+ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
76+ int retv;
77+ retv = HeavyOSECPUMain(argc, (char **)argv);
78+ [NSApp terminate:self];
79+ [pool release];
80+ exit(retv);
81+}
82+
83+- (void)createThread : (int)_argc args:(const unsigned char **)_argv
84+{
85+ argc = _argc;
86+ argv = _argv;
87+ NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(runApp) object:nil] autorelease];
88+ [thread start];
89+}
90+
91+- (BOOL)windowShouldClose:(id)sender
92+{
93+ *winClosed = 1;
94+ return YES;
95+}
96+
97+- (void)openWin:(unsigned char *)buf sx:(int)sx sy:(int) sy winClosed:(char *)_winClosed
98+{
99+
100+ NSWindow* window = [[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, sx, sy) styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask backing: NSBackingStoreBuffered defer: NO];
101+ [window setTitle: @"osecpu"];
102+ [window center];
103+ [window makeKeyAndOrderFront:nil];
104+ [window setReleasedWhenClosed:YES];
105+ window.delegate = self;
106+ winClosed = _winClosed;
107+
108+ _view = [[OSECPUView alloc] initWithFrame:NSMakeRect(0,0,sx,sy) buf:buf sx:sx sy:sy];
109+ [window.contentView addSubview:_view];
110+}
111+
112+- (void)flushWin : (NSRect)rect
113+{
114+ [_view drawRect:rect];
115+}
116+
117+@end
118+
119+id objc_main;
120+
121+int main(int argc, char **argv)
122+{
123+ objc_main = [[Main alloc] init];
124+
125+ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
126+ app = [[NSApplication alloc] init];
127+ [objc_main createThread:argc args:(const unsigned char **)argv];
128+ [app run];
129+ [pool release];
130+ return 0;
131+}
132+
133+void drv_openWin(int sx, int sy, unsigned char *buf, char *winClosed)
134+{
135+ [objc_main openWin:buf sx:sx sy:sy winClosed:winClosed];
136+}
137+
138+void drv_flshWin(int sx, int sy, int x0, int y0)
139+{
140+ [objc_main flushWin:NSMakeRect(x0,y0,sx,sy)];
141+}
142+
143+void drv_sleep(int msec)
144+{
145+ [NSThread sleepForTimeInterval:0.001*msec];
146+ return;
147+}
148+
149+
150+#elif (DRV_OSNUM == 0x0003)
151+
152+#error "Your OS is not supported."
153+
154+#elif (DRV_OSNUM == 0x0001)
155+//
156+// for Windows 32-bit
157+//
158+#include <windows.h>
159+
160+#define TIMER_ID 1
161+#define TIMER_INTERVAL 10
162+
163+struct BLD_WORK {
164+ HINSTANCE hi;
165+ HWND hw;
166+ BITMAPINFO bmi;
167+ int tmcount1, tmcount2, flags, smp; /* bit0: 終了 */
168+ HANDLE mtx;
169+ char *winClosed;
170+};
171+
172+struct BLD_WORK bld_work;
173+
174+struct BL_WIN {
175+ int xsiz, ysiz, *buf;
176+};
177+
178+struct BL_WORK {
179+ struct BL_WIN win;
180+ jmp_buf jb;
181+ int csiz_x, csiz_y, cx, cy, col0, col1, tabsiz, slctwin;
182+ int tmcount, tmcount0, mod, rand_seed;
183+ int *cbuf;
184+ unsigned char *ftyp;
185+ unsigned char **fptn;
186+ int *ccol, *cbak;
187+ int *kbuf, kbuf_rp, kbuf_wp, kbuf_c;
188+};
189+
190+struct BL_WORK bl_work;
191+
192+#define BL_SIZ_KBUF 8192
193+
194+#define BL_WAITKEYF 0x00000001
195+#define BL_WAITKEYNF 0x00000002
196+#define BL_WAITKEY 0x00000003
197+#define BL_GETKEY 0x00000004
198+#define BL_CLEARREP 0x00000008
199+#define BL_DELFFF 0x00000010
200+
201+#define BL_KEYMODE 0x00000000 // 作りかけ, make/remake/breakが見えるかどうか
202+
203+#define w bl_work
204+#define dw bld_work
205+
206+void bld_openWin(int x, int y, char *winClosed);
207+void bld_flshWin(int sx, int sy, int x0, int y0);
208+LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp);
209+void bl_cls();
210+int bl_iCol(int i);
211+void bl_readyWin(int n);
212+
213+static HANDLE threadhandle;
214+
215+int main(int argc, char **argv)
216+{
217+ // Program entry point
218+ return HeavyOSECPUMain(argc, argv);
219+}
220+
221+void *mallocRWE(int bytes)
222+{
223+ void *p = malloc(bytes);
224+ DWORD dmy;
225+ VirtualProtect(p, bytes, PAGE_EXECUTE_READWRITE, &dmy);
226+ return p;
227+}
228+
229+static int winthread(void *dmy)
230+{
231+ WNDCLASSEX wc;
232+ RECT r;
233+ int i, x, y;
234+ MSG msg;
235+
236+ x = dw.bmi.bmiHeader.biWidth;
237+ y = -dw.bmi.bmiHeader.biHeight;
238+
239+ wc.cbSize = sizeof (WNDCLASSEX);
240+ wc.style = CS_HREDRAW | CS_VREDRAW;
241+ wc.lpfnWndProc = WndProc;
242+ wc.cbClsExtra = 0;
243+ wc.cbWndExtra = 0;
244+ wc.hInstance = dw.hi;
245+ wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION),
246+ IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
247+ wc.hIconSm = wc.hIcon;
248+ wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW),
249+ IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
250+ wc.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
251+ wc.lpszMenuName = NULL;
252+ wc.lpszClassName = "WinClass";
253+ if (RegisterClassEx(&wc) == 0)
254+ return 1;
255+ r.left = 0;
256+ r.top = 0;
257+ r.right = x;
258+ r.bottom = y;
259+ AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
260+ x = r.right - r.left;
261+ y = r.bottom - r.top;
262+
263+ char *t = "osecpu";
264+
265+ dw.hw = CreateWindowA("WinClass", t, WS_OVERLAPPEDWINDOW,
266+ CW_USEDEFAULT, CW_USEDEFAULT, x, y, NULL, NULL, dw.hi, NULL);
267+ if (dw.hw == NULL)
268+ return 1;
269+ ShowWindow(dw.hw, SW_SHOW);
270+ UpdateWindow(dw.hw);
271+ SetTimer(dw.hw, TIMER_ID, TIMER_INTERVAL, NULL);
272+ SetTimer(dw.hw, TIMER_ID + 1, TIMER_INTERVAL * 10, NULL);
273+ SetTimer(dw.hw, TIMER_ID + 2, TIMER_INTERVAL * 100, NULL);
274+ dw.flags |= 2 | 4;
275+
276+ for (;;) {
277+ i = GetMessage(&msg, NULL, 0, 0);
278+ if (i == 0 || i == -1) /* エラーもしくは終了メッセージ */
279+ break;
280+ /* そのほかはとりあえずデフォルト処理で */
281+ TranslateMessage(&msg);
282+ DispatchMessage(&msg);
283+ }
284+ // PostQuitMessage(0);
285+ dw.flags |= 1; /* 終了, bld_waitNF()が見つける */
286+ if (dw.winClosed != NULL)
287+ *dw.winClosed = 1;
288+ return 0;
289+}
290+
291+void bld_openWin(int sx, int sy, char *winClosed)
292+{
293+ static int i;
294+
295+ dw.bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
296+ dw.bmi.bmiHeader.biWidth = sx;
297+ dw.bmi.bmiHeader.biHeight = -sy;
298+ dw.bmi.bmiHeader.biPlanes = 1;
299+ dw.bmi.bmiHeader.biBitCount = 32;
300+ dw.bmi.bmiHeader.biCompression = BI_RGB;
301+ dw.winClosed = winClosed;
302+
303+ threadhandle = CreateThread(NULL, 0, (void *)&winthread, NULL, 0, (void *)&i);
304+
305+ return;
306+}
307+
308+void drv_flshWin(int sx, int sy, int x0, int y0)
309+{
310+ InvalidateRect(dw.hw, NULL, FALSE);
311+ UpdateWindow(dw.hw);
312+ return;
313+}
314+
315+LRESULT CALLBACK WndProc(HWND hw, unsigned int msg, WPARAM wp, LPARAM lp)
316+{
317+ int i, j;
318+ if (msg == WM_PAINT) {
319+ PAINTSTRUCT ps;
320+ HDC hdc = BeginPaint(dw.hw, &ps);
321+ SetDIBitsToDevice(hdc, 0, 0, w.win.xsiz, w.win.ysiz,
322+ 0, 0, 0, w.win.ysiz, w.win.buf, &dw.bmi, DIB_RGB_COLORS);
323+ EndPaint(dw.hw, &ps);
324+ }
325+ if (msg == WM_DESTROY) {
326+ PostQuitMessage(0);
327+ return 0;
328+ }
329+ if (msg == WM_TIMER && wp == TIMER_ID) {
330+ w.tmcount += TIMER_INTERVAL;
331+ return 0;
332+ }
333+ if (msg == WM_TIMER && wp == TIMER_ID + 1) {
334+ dw.tmcount1 += TIMER_INTERVAL * 10;
335+ w.tmcount = dw.tmcount1;
336+ return 0;
337+ }
338+ if (msg == WM_TIMER && wp == TIMER_ID + 2) {
339+ dw.tmcount2 += TIMER_INTERVAL * 100;
340+ w.tmcount = dw.tmcount1 = dw.tmcount2;
341+ return 0;
342+ }
343+ if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
344+ i = -1;
345+
346+ if (wp == VK_RETURN) i = KEY_ENTER;
347+ if (wp == VK_ESCAPE) i = KEY_ESC;
348+ if (wp == VK_BACK) i = KEY_BACKSPACE;
349+ if (wp == VK_TAB) i = KEY_TAB;
350+ if (wp == VK_PRIOR) i = KEY_PAGEUP;
351+ if (wp == VK_NEXT) i = KEY_PAGEDWN;
352+ if (wp == VK_END) i = KEY_END;
353+ if (wp == VK_HOME) i = KEY_HOME;
354+ if (wp == VK_LEFT) i = KEY_LEFT;
355+ if (wp == VK_RIGHT) i = KEY_RIGHT;
356+ if (wp == VK_UP) i = KEY_UP;
357+ if (wp == VK_DOWN) i = KEY_DOWN;
358+ if (wp == VK_INSERT) i = KEY_INS;
359+ if (wp == VK_DELETE) i = KEY_DEL;
360+ j &= 0;
361+ if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
362+ if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
363+ if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
364+ if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
365+ if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
366+ if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
367+ if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
368+ if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
369+ if (j != 0) {
370+ if ('A' <= wp && wp <= 'Z') i = wp;
371+ }
372+ if (i != -1) {
373+ putKeybuf(i | j);
374+ // bl_putKeyB(1, &i);
375+ return 0;
376+ }
377+ }
378+ if (msg == WM_KEYUP) {
379+ i = 0xfff;
380+ // bl_putKeyB(1, &i);
381+ }
382+ if (msg == WM_CHAR) {
383+ i = 0;
384+ if (' ' <= wp && wp <= 0x7e) {
385+ i = wp;
386+ j &= 0;
387+ if ((GetKeyState(VK_LCONTROL) & (1 << 15)) != 0) j |= 1 << 17;
388+ if ((GetKeyState(VK_LMENU) & (1 << 15)) != 0) j |= 1 << 18;
389+ if ((GetKeyState(VK_RCONTROL) & (1 << 15)) != 0) j |= 1 << 25;
390+ if ((GetKeyState(VK_RMENU) & (1 << 15)) != 0) j |= 1 << 26;
391+ if ((GetKeyState(VK_RSHIFT) & (1 << 15)) != 0) i |= 1 << 24;
392+ if ((GetKeyState(VK_LSHIFT) & (1 << 15)) != 0) i |= 1 << 16;
393+ if ((GetKeyState(VK_NUMLOCK) & (1 << 0)) != 0) i |= 1 << 22;
394+ if ((GetKeyState(VK_CAPITAL) & (1 << 0)) != 0) i |= 1 << 23;
395+ if (('A' <= wp && wp <= 'Z') || ('a' <= wp && wp <= 'z')) {
396+ if (j != 0) {
397+ i |= j;
398+ i &= ~0x20;
399+ }
400+ }
401+ putKeybuf(i);
402+ // bl_putKeyB(1, &i);
403+ return 0;
404+ }
405+ }
406+ return DefWindowProc(hw, msg, wp, lp);
407+}
408+
409+void drv_openWin(int sx, int sy, UCHAR *buf, char *winClosed)
410+{
411+ w.win.buf = (int *)buf;
412+ w.win.xsiz = sx;
413+ w.win.ysiz = sy;
414+ bld_openWin(sx, sy, winClosed);
415+ return;
416+}
417+
418+void drv_sleep(int msec)
419+{
420+ Sleep(msec);
421+ // MsgWaitForMultipleObjects(1, &threadhandle, FALSE, msec, QS_ALLINPUT);
422+ /* 勉強不足でまだ書き方が分かりません! */
423+ return;
424+}
425+
426+#else
427+
428+#error "Illegal OS type. Edit osecpu.h and look at DRV_OSNUM"
429+
428430 #endif
\ No newline at end of file
--- a/function.c
+++ b/function.c
@@ -1,719 +1,738 @@
1-#include "osecpu.h"
2-
3-extern unsigned char fontdata[]; // @fontdata.c
4-
5-const char *searchArg(int argc, const char **argv, const char *tag, int i)
6-{
7- int j, l;
8- const char *r = NULL;
9- if (tag != NULL) {
10- l = (int)strlen(tag);
11- for (j = 1; j < argc; j++) {
12- if (strncmp(argv[j], tag, l) == 0) {
13- r = argv[j] + l;
14- if (i == 0){
15- break;
16- }
17- i--;
18- }
19- }
20- }
21- else {
22- for (j = 1; j < argc; j++) {
23- if (strchr(argv[j], ':') == NULL) {
24- r = argv[j];
25- if (i == 0){
26- break;
27- }
28- i--;
29- }
30- }
31- }
32- if (i != 0){
33- r = NULL;
34- }
35- return r;
36-}
37-
38-void devFunc0001(int len, unsigned char *puc, HOSECPU_RuntimeEnvironment *r)
39-{
40- while (len > 0) {
41- putOsaskChar(*puc++, r);
42- len--;
43- }
44- return;
45-}
46-
47-// drawString
48-void devFunc0006(int mod, int sx, int sy, int x, int y, int c, int len, unsigned char *puc, HOSECPU_RuntimeEnvironment *r)
49-{
50- int xx;
51- int yy;
52- int i, ddx, ddy, j, ch, dx, dy;
53-
54- if (sy == 0){
55- sy = sx;
56- }
57- xx = x + sx * 8;
58- yy = y + sy * 16;
59- if (xx <= 0 || xx > mainWindow.xsize || yy <= 0 || yy > mainWindow.ysize){
60- (*(r->errHndl))(r);
61- }
62- if (x < 0 || x >= mainWindow.xsize || y < 0 || y >= mainWindow.ysize){
63- (*(r->errHndl))(r);
64- }
65-
66-
67- if ((mod & 3) == 0 && sx == 1 && sy == 1) {
68- // メジャーケースを高速化.
69- for (i = 0; i < len; i++) {
70- ch = puc[i];
71- if (0x10 <= ch && ch <= 0x1f)
72- ch = "0123456789ABCDEF"[ch & 0x0f];
73- for (dy = 0; dy < 16; dy++) {
74- j = fontdata[(ch - ' ') * 16 + dy];
75- for (dx = 0; dx < 8; dx++) {
76- if ((j & (0x80 >> dx)) != 0){
77- mainWindow.vram[(x + dx) + (y + dy) * mainWindow.xsize] = c;
78- }
79- }
80- }
81- x += 8;
82- }
83- return;
84- }
85- for (i = 0; i < len; i++) {
86- ch = puc[i];
87- if (0x10 <= ch && ch <= 0x1f)
88- ch = "0123456789ABCDEF"[ch & 0x0f];
89- for (dy = 0; dy < 16; dy++) {
90- j = fontdata[(ch - ' ') * 16 + dy];
91- for (ddy = 0; ddy < sy; ddy++) {
92- for (dx = 0; dx < 8; dx++) {
93- if ((j & (0x80 >> dx)) != 0) {
94- for (ddx = 0; ddx < sx; ddx++) {
95- switch (mod & 3) {
96- case 0:
97- mainWindow.vram[x + y * mainWindow.xsize] = c;
98- break;
99- case 1:
100- mainWindow.vram[x + y * mainWindow.xsize] |= c;
101- break;
102- case 2:
103- mainWindow.vram[x + y * mainWindow.xsize] ^= c;
104- break;
105- case 3:
106- mainWindow.vram[x + y * mainWindow.xsize] &= c;
107- break;
108- }
109- x++;
110- }
111- } else{
112- x += sx;
113- }
114- }
115- x -= sx * 8;
116- y++;
117- }
118- }
119- x += sx * 8;
120- y -= sy * 16;
121- }
122- return;
123-}
124-
125-void devFunc0004(int mod, int x0, int y0, int x1, int y1, int c)
126-{
127- int x, y;
128- if (mod == 0) {
129- for (y = y0; y <= y1; y++) {
130- for (x = x0; x <= x1; x++) {
131- mainWindow.vram[x + y * mainWindow.xsize] = c;
132- }
133- }
134- }
135- else {
136- for (y = y0; y <= y1; y++) {
137- for (x = x0; x <= x1; x++) {
138- switch (mod) {
139- case 1:
140- mainWindow.vram[x + y * mainWindow.xsize] |= c;
141- break;
142- case 2:
143- mainWindow.vram[x + y * mainWindow.xsize] ^= c;
144- break;
145- case 3:
146- mainWindow.vram[x + y * mainWindow.xsize] &= c;
147- break;
148- }
149- }
150- }
151- }
152- return;
153-}
154-
155-int devFunc0016(int buflen, unsigned char *buf, int plen, unsigned char *p, int qlen, int *q, HOSECPU_RuntimeEnvironment *r)
156-{
157- int i = 0, base, j, k;
158- char sign;
159-
160- while (plen > 0) {
161- if (i >= buflen){
162- (*(r->errHndl))(r);
163- }
164- if (*p != 0x01) {
165- buf[i++] = *p++;
166- plen--;
167- continue;
168- }
169- p++;
170- plen--;
171- if (qlen < 4){
172- (*(r->errHndl))(r);
173- }
174- base = q[0];
175- sign = 0;
176- if (base == 0){
177- base = 16;
178- }
179-#if (REVISION == 1)
180- if (base == -3){
181- base = 10;
182- }
183-#endif
184- if (base == -1){
185- base = 10;
186- }
187- if (base < 0 || base > 16){
188- (*(r->errHndl))(r);
189- }
190- if (q[1] + i > buflen){
191- (*(r->errHndl))(r);
192- }
193- j = q[3];
194- if ((q[2] & 4) == 0) {
195- // jは符号付き整数.
196- if ((q[2] & 8) != 0 && j > 0){
197- sign = '+';
198- }
199- if (j < 0) {
200- sign = '-'; j *= -1;
201- }
202- } else{
203- // jは符号無し整数.
204- if ((q[2] & 8) != 0 && j != 0){
205- sign = '+';
206- }
207- }
208- for (k = q[1] - 1; k >= 0; k--) {
209- buf[i + k] = (j % base) + 0x10;
210- j = ((unsigned)j) / base;
211- }
212- k = 0;
213- if ((q[2] & 2) == 0 && j == 0) {
214- for (k = 0; k < q[1] - 1; k++) {
215- if (buf[i + k] != 0x10){
216- break;
217- }
218- buf[i + k] = ' ';
219- }
220- }
221- if (sign != 0) {
222- if (k > 0){
223- k--;
224- }
225- buf[i + k] = sign;
226- }
227- if ((q[2] & 1) != 0 && buf[i] == ' ') {
228- for (j = 0; k < q[1]; k++, j++){
229- buf[i + j] = buf[i + k];
230- }
231- i += j;
232- } else{
233- i += q[1];
234- }
235- qlen -= 4;
236- q += 4;
237- }
238- return i;
239-}
240-
241-void devFunc(HOSECPU_RuntimeEnvironment *r)
242-{
243- FILE *fp;
244- int i, c;
245- int x, y, len, dx, dy;
246- unsigned char *puc;
247- unsigned char pucbuf[256];
248-
249- //サイズを節約するためにEBPを128バイトずらしているのを元に戻す
250- r = (HOSECPU_RuntimeEnvironment *) (((char *)r) - 128);
251-
252- if (r->winClosed != 0){
253- longjmp(*(r->setjmpEnv), 1);
254- }
255- if (0xff44 <= r->ireg[0x30] && r->ireg[0x30] <= 0xff48) {
256- if (mainWindow.vram == NULL) {
257- mainWindow.xsize = 640;
258- mainWindow.ysize = 480;
259- mainWindow.vram = malloc(640 * 480 * 4);
260- drv_openWin(640, 480, (void *)mainWindow.vram, &r->winClosed);
261- r->autoSleep = 1;
262- for (i = 640 * 480 - 1; i >= 0; i--){
263- mainWindow.vram[i] = 0;
264- }
265- }
266- }
267-
268- switch (r->ireg[0x30]){
269- case 0xff00:
270- printf("R31=%d(dec)\n", r->ireg[0x31]);
271- break;
272-
273- case 0xff01: // junkApi_fopenRead(_filesize, _p, arg) R31=arg; PCALL(P28); _filesize=R30; _p=P31
274-
275- /* return: R30, P31 */
276- if (r->buf0 == NULL){
277- r->buf0 = malloc(1024 * 1024);
278- }
279- if (r->mainArgc <= r->ireg[0x31]) {
280- fprintf(stderr, "devFunc: error: R30=ff01: argc error: R31=%08X\n", r->ireg[0x31]);
281- exit(1);
282- }
283- fp = fopen(r->mainArgv[r->ireg[0x31]], "rb");
284- if (fp == NULL) {
285- fprintf(stderr, "devFunc: error: R30=ff01: fopen error: '%s'\n", r->mainArgv[r->ireg[0x31]]);
286- exit(1);
287- }
288- i = (int)fread(r->buf0, 1, 1024 * 1024 - 4, fp);
289- if (i >= 1024 * 1024 - 4 || i < 0) {
290- fprintf(stderr, "devFunc: error: R30=ff01: fread error: '%s'\n", r->mainArgv[r->ireg[0x31]]);
291- exit(1);
292- }
293- fclose(fp);
294- r->preg[0x31].p = r->buf0;
295- r->preg[0x31].p0 = r->buf0;
296- r->preg[0x31].p1 = r->buf0 + i;
297- r->preg[0x31].typ = 3; // T_UINT8
298- r->ireg[0x30] = i;
299- break;
300-
301- case 0xff02: // junkApi_fopenWrite(arg, filesize, p) R31=arg; R32=filesize; P31=p;
302- /* return: none */
303- if (r->mainArgc <= r->ireg[0x31]) {
304- fprintf(stderr, "devFunc: error: R30=ff02: argc error: R31=%08X\n", r->ireg[0x31]);
305- exit(1);
306- }
307- fp = fopen(r->mainArgv[r->ireg[0x31]], "wb");
308- if (fp == NULL) {
309- fprintf(stderr, "devFunc: error: R30=ff02: fopen error: '%s'\n", r->mainArgv[r->ireg[0x31]]);
310- exit(1);
311- }
312- if (r->ireg[0x32] >= 1024 * 1024 || r->ireg[0x32] < 0){
313- fprintf(stderr, "devFunc: error: R30=ff02: fwrite error: R02=%08X\n", r->ireg[0x32]);
314- exit(1);
315- }
316- fwrite(r->preg[0x31].p, 1, r->ireg[0x32], fp);
317- fclose(fp);
318- break;
319-
320- case 0xff03: // junkApi_allocBuf(_p) _p=P31
321- /* return: P31 */
322- if (r->buf1 == NULL){
323- r->buf1 = malloc(1024 * 1024);
324- }
325- r->preg[0x31].p = r->buf1;
326- r->preg[0x31].p0 = r->buf1;
327- r->preg[0x31].p1 = r->buf1 + 1024 * 1024;
328- break;
329-
330- case 0xff04:
331- printf("P31.(p-p0)=%d(dec)\n", (int)(r->preg[0x31].p - r->preg[0x31].p0));
332- break;
333-
334- case 0xff05: // junkApi_writeStdout(len, p) R31=len; P31=p;
335- fwrite(r->preg[0x31].p, 1, r->ireg[0x31], stdout);
336- break;
337-
338- case 0xff06: // jnukApi_exit(i) R31=i;
339- // R31はリターンコード.
340- // これを反映すべきだが、現状は手抜きでいつも正常終了.
341- longjmp(*(r->setjmpEnv), 1);
342- break;
343-
344- case 0xff07: // junkApi_putConstString0(s) DB(0xff,0x00,0x00); DB%(s,0x00);
345- // マシになった文字列表示.OSASK文字列に対応.offにすれば通常の文字列処理もできる.現状はonのみサポート.
346- checkString(r, 0x31, 0x31);
347- devFunc0001(r->ireg[0x31], r->preg[0x31].p, r);
348- break;
349-
350- case 0xff08: // junkApi_jitc2(_rc, _p, mod, lev, di1, len, s) R31=mod; R32=lev; R33=di1; R34=len; P31=s; _rc=R30; _p=P31
351- // JITC on JITC
352- // R31: 言語(back-end, front-end, ...
353- // R32: level
354- // R33: debugInfo1
355- checkString(r, 0x34, 0x31);
356- if (r->ireg[0x33] < 0 || r->ireg[0x33] > 15 || r->debugInfo1 > 15){
357- (*(r->errHndl))(r);
358- }
359- for (i = 0; i < r->maxLabels; i++){
360- r->label[i].opt = 0;
361- }
362- puc = r->preg[0x31].p;
363- i = jitCompiler(r->jitbuf, r->jitbuf1, puc, puc + r->ireg[0x34], puc, r->label, r->maxLabels, r->ireg[0x32], di1_serial, JITC_NOSTARTUP + 0);
364- if (i == 0) {
365- i = jitCompiler(r->jitbuf, r->jitbuf1, puc, puc + r->ireg[0x34], puc, r->label, r->maxLabels, r->ireg[0x32], di1_serial, JITC_NOSTARTUP + JITC_PHASE1 + 0);
366- if (i >= 0) {
367- r->mapDi1s[r->debugInfo1][r->ireg[0x33]] = di1_serial;
368- di1_serial++;
369- r->ireg[0x30] = 0;
370- r->preg[0x31].p = r->jitbuf;
371- r->preg[0x31].typ = 0; // TYP_CODE
372- r->preg[0x31].p0 = r->jitbuf;
373- r->preg[0x31].p1 = r->jitbuf + 1;
374- //int j; for (j = 0; j < i; j++) printf("%02X ", r->jitbuf[j]); putchar('\n');
375- r->jitbuf += i;
376- static unsigned char ff08_ret[3] = { 0x1e, 0x3f, 0x30 };
377- i = jitCompiler(r->jitbuf, r->jitbuf1, ff08_ret, ff08_ret + 3, puc, r->label, r->maxLabels, r->ireg[0x32], -1, JITC_NOSTARTUP + JITC_PHASE1 + 0);
378- r->jitbuf += i;
379- break;
380- }
381- }
382- r->ireg[0x30] = -1;
383- break;
384-
385- case 0xff09: //putStringDec
386- // たぶんbit7を使ったテキストはうまく処理できない(これはもはや仕様にしても問題ないかも).
387- checkString(r, 0x31, 0x31);
388- len = devFunc0016(sizeof pucbuf, pucbuf, r->ireg[0x31], r->preg[0x31].p, r->ireg[0x32], (int *)r->preg[0x32].p, r);
389- devFunc0001(len, pucbuf, r);
390- break;
391-
392- case 0xff40: // junkApi_openWin(xsiz, ysiz) R31=xsiz; R32=ysiz;
393- /* R31とR32でサイズを指定 */
394- mainWindow.xsize = r->ireg[0x31];
395- mainWindow.ysize = r->ireg[0x32];
396- if (mainWindow.xsize <= 0 || mainWindow.ysize <= 0){
397- (*(r->errHndl))(r);
398- }
399- r->preg[0x31].p = (void *)(mainWindow.vram = malloc(mainWindow.xsize * mainWindow.ysize * 4));
400- r->preg[0x31].p0 = r->preg[0x31].p;
401- r->preg[0x31].p1 = r->preg[0x31].p + mainWindow.xsize * mainWindow.ysize * 4;
402- drv_openWin(r->ireg[0x31], r->ireg[0x32], r->preg[0x31].p, &r->winClosed);
403- // drv_flshWin(r->ireg[1], r->ireg[2], 0, 0);
404- r->autoSleep = 1;
405- for (i = mainWindow.xsize * mainWindow.ysize - 1; i >= 0; i--){
406- mainWindow.vram[i] = 0;
407- }
408- break;
409-
410- case 0xff41: // junkApi_flushWin(xsiz, ysiz, x0, y0) R31=xsiz; R32=ysiz; R33=x0; R34=y0
411- /* R31とR32でサイズを指定、R33とR34でx0,y0指定 */
412- if (r->ireg[0x31] == -1) {
413- r->ireg[0x31] = mainWindow.xsize; r->ireg[0x33] &= 0;
414- }
415- if (r->ireg[0x32] == -1) {
416- r->ireg[0x32] = mainWindow.ysize; r->ireg[0x34] &= 0;
417- }
418- checkRect(r, 0x31);
419- drv_flshWin(r->ireg[0x31], r->ireg[0x32], r->ireg[0x33], r->ireg[0x34]);
420- break;
421-
422- case 0xff42: // junkApi_sleep(opt, msec) R31=opt; R32=msec
423- if (r->ireg[0x32] == -1) {
424- r->autoSleep = 1;
425- longjmp(*(r->setjmpEnv), 1);
426- }
427- if (r->ireg[0x32] < 0){
428- (*(r->errHndl))(r);
429- }
430- r->autoSleep = 0;
431- if ((r->ireg[0x31] & 1) == 0 && mainWindow.vram != NULL){
432- drv_flshWin(mainWindow.xsize, mainWindow.ysize, 0, 0);
433- }
434- for (;;) {
435- if (r->winClosed != 0){
436- longjmp(*(r->setjmpEnv), 1);
437- }
438- drv_sleep(r->ireg[0x32]);
439- if ((r->ireg[0x31] & 2) != 0 && keybuf_c <= 0){
440- continue;
441- }
442- break;
443- }
444- break;
445-
446- case 0xff43: // junkApi_inkey(_i, mod) R31=mod; _i=R30
447- // 1:peek
448- // 2:stdin
449- // 4,8: ソース指定.
450- // 16: shift, lock系を有効化.
451- // 32: 左右のshift系を区別する.
452- if (r->ireg[0x31] == 2) { // なぜ3にしなかったのか...
453- r->ireg[0x30] = fgetc(stdin);
454- if (r->ireg[0x30] == EOF){
455- r->ireg[0x30] = -1;
456- }
457- break;
458- }
459- r->ireg[0x30] |= -1;
460- if (keybuf_c > 0) {
461- r->ireg[0x30] = keybuf[keybuf_r];
462- if ((r->ireg[0x31] & 16) == 0){
463- r->ireg[0x30] &= 0x3e3effff;
464- }
465- if ((r->ireg[0x31] & 32) == 0){
466- r->ireg[0x30] |= (r->ireg[0x30] >> 8) & 0xff0000;
467- }
468- if ((r->ireg[0x31] & 1) != 0) {
469- keybuf_c--;
470- keybuf_r = (keybuf_r + 1) & (KEYBUFSIZ - 1);
471- }
472- }
473- r->ireg[0x32] = r->ireg[0x33] = 0;
474- if (r->ireg[0x30] == 4132) r->ireg[0x32]--;
475- if (r->ireg[0x30] == 4133) r->ireg[0x33]--;
476- if (r->ireg[0x30] == 4134) r->ireg[0x32]++;
477- if (r->ireg[0x30] == 4135) r->ireg[0x33]++;
478- break;
479-
480- case 0xff44: // junkApi_drawPoint(mod, x, y, c) R31=mod; R32=x; R33=y; R34=c
481- c = loadColor(r, 0x34);
482- if (r->ireg[0x32] < 0 || r->ireg[0x32] >= mainWindow.xsize ||
483- r->ireg[0x33] < 0 || r->ireg[0x33] >= mainWindow.ysize){
484- (*(r->errHndl))(r);
485- }
486-
487- switch ((r->ireg[0x31] & 3)) {
488- case 0:
489- mainWindow.vram[r->ireg[0x32] + r->ireg[0x33] * mainWindow.xsize] = c;
490- break;
491- case 1:
492- mainWindow.vram[r->ireg[0x32] + r->ireg[0x33] * mainWindow.xsize] |= c;
493- break;
494- case 2:
495- mainWindow.vram[r->ireg[0x32] + r->ireg[0x33] * mainWindow.xsize] ^= c;
496- break;
497- case 3:
498- mainWindow.vram[r->ireg[0x32] + r->ireg[0x33] * mainWindow.xsize] &= c;
499- break;
500- }
501- break;
502-
503- case 0xff45: // junkApi_drawLine(mod, x0, y0, x1, y1, c) DB(0xfe,0x05,0x01); DDBE(0x0003); R30=0xff45; R31=mod; R32=x0; R33=y0; R34=x1; R35=y1; R36=c
504- //drawLine
505- c = loadColor(r, 0x36);
506- if (r->ireg[0x32] < 0 || r->ireg[0x32] >= mainWindow.xsize ||
507- r->ireg[0x33] < 0 || r->ireg[0x33] >= mainWindow.ysize){
508- (*(r->errHndl))(r);
509- }
510- if (r->ireg[0x34] < 0 || r->ireg[0x34] >= mainWindow.xsize ||
511- r->ireg[0x35] < 0 || r->ireg[0x35] >= mainWindow.ysize){
512- (*(r->errHndl))(r);
513- }
514- dx = r->ireg[0x34] - r->ireg[0x32];
515- dy = r->ireg[0x35] - r->ireg[0x33];
516- x = r->ireg[0x32] << 10;
517- y = r->ireg[0x33] << 10;
518- if (dx < 0){
519- dx = -dx;
520- }
521- if (dy < 0){
522- dy = -dy;
523- }
524- if (dx >= dy) {
525- len = dx + 1; dx = 1024;
526- if (r->ireg[0x32] > r->ireg[0x34]){
527- dx *= -1;
528- }
529- if (r->ireg[0x33] > r->ireg[0x35]){
530- dy *= -1;
531- }
532- dy = (dy << 10) / len;
533- } else {
534- len = dy + 1; dy = 1024;
535- if (r->ireg[0x33] > r->ireg[0x35]){
536- dy *= -1;
537- }
538- if (r->ireg[0x32] > r->ireg[0x34]){
539- dx *= -1;
540- }
541- dx = (dx << 10) / len;
542- }
543- if ((r->ireg[0x31] & 3) == 0) {
544- for (i = 0; i < len; i++) {
545- mainWindow.vram[(x >> 10) + (y >> 10) * mainWindow.xsize] = c;
546- x += dx;
547- y += dy;
548- }
549- break;
550- }
551- for (i = 0; i < len; i++) {
552- // if ((r->ireg[0x31] & 3) == 0) vram[(x >> 10) + (y >> 10) * v_xsiz] = c;
553- switch ((r->ireg[0x31] & 3)) {
554- case 1:
555- mainWindow.vram[(x >> 10) + (y >> 10) * mainWindow.xsize] |= c;
556- break;
557- case 2:
558- mainWindow.vram[(x >> 10) + (y >> 10) * mainWindow.xsize] ^= c;
559- break;
560- case 3:
561- mainWindow.vram[(x >> 10) + (y >> 10) * mainWindow.xsize] &= c;
562- break;
563- default:
564- break;
565- }
566- x += dx;
567- y += dy;
568- }
569- break;
570-
571- case 0xff46: // fillRect(opt:R31, xsiz:R32, ysiz:R33, x0:R34, y0:R35, c:R36)
572- c = loadColor(r, 0x36);
573- if (r->ireg[0x32] == -1) { r->ireg[0x32] = mainWindow.xsize; r->ireg[0x34] &= 0; }
574- if (r->ireg[0x33] == -1) { r->ireg[0x33] = mainWindow.ysize; r->ireg[0x35] &= 0; }
575- checkRect(r, 0x32);
576- int mod3 = r->ireg[0x31] & 3, x0 = r->ireg[0x34], y0 = r->ireg[0x35], x1 = r->ireg[0x34] + r->ireg[0x32] - 1, y1 = r->ireg[0x35] + r->ireg[0x33] - 1;
577- if ((r->ireg[0x31] & 0x20) == 0) {
578- devFunc0004(mod3, x0, y0, x1, y1, c);
579- } else { // drawRect
580- devFunc0004(mod3, x0, y0, x1, y0, c);
581- devFunc0004(mod3, x0, y1, x1, y1, c);
582- devFunc0004(mod3, x0, y0, x0, y1, c);
583- devFunc0004(mod3, x1, y0, x1, y1, c);
584- }
585- break;
586-
587- case 0xff47: // fillOval(opt:R31, xsiz:R32, ysiz:R33, x0:R34, y0:R35, c:R36)
588- // これの計算精度はアーキテクチャに依存する.
589- c = loadColor(r, 0x36);
590- if (r->ireg[0x32] == -1) {
591- r->ireg[0x32] = mainWindow.xsize; r->ireg[0x34] &= 0;
592- }
593- if (r->ireg[0x33] == -1) {
594- r->ireg[0x33] = mainWindow.ysize; r->ireg[0x35] &= 0;
595- }
596- checkRect(r, 0x32);
597- double dcx = 0.5 * (r->ireg[0x32] - 1);
598- double dcy = 0.5 * (r->ireg[0x33] - 1);
599- double dcxy = (dcx + 0.5) * (dcy + 0.5) - 0.1;
600- dcxy *= dcxy;
601- mod3 = r->ireg[0x31] & 3;
602- x1 = r->ireg[0x32];
603- y1 = r->ireg[0x33];
604- if (mod3 == 0 && (r->ireg[0x31] & 0x20) == 0) {
605- for (y = 0; y < y1; y++) {
606- double dty = (y - dcy) * dcx;
607- for (x = 0; x < x1; x++) {
608- double dtx = (x - dcx) * dcy;
609- if (dtx * dtx + dty * dty > dcxy){
610- continue;
611- }
612- mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] = c;
613- }
614- }
615- } else {
616-#define DRAWOVALPARAM 1
617- double dcx1 = 0.5 * (r->ireg[0x32] - (1 + DRAWOVALPARAM * 2));
618- double dcy1 = 0.5 * (r->ireg[0x33] - (1 + DRAWOVALPARAM * 2));
619- double dcxy1 = (dcx1 + 0.5) * (dcy1 + 0.5) - 0.1;
620- dcxy1 *= dcxy1;
621- for (y = 0; y < y1; y++) {
622- double dty = (y - dcy) * dcx;
623- double dty1 = (y - dcy) * dcx1;
624- for (x = 0; x < x1; x++) {
625- double dtx = (x - dcx) * dcy;
626- double dtx1 = (x - dcx) * dcy1;
627- if (dtx * dtx + dty * dty > dcxy){
628- continue;
629- }
630- if (DRAWOVALPARAM <= x && x < x1 - DRAWOVALPARAM && DRAWOVALPARAM <= y && y < y1 - DRAWOVALPARAM) {
631- if (dtx1 * dtx1 + dty1 * dty1 < dcxy1){
632- continue;
633- }
634- }
635- switch (mod3) {
636- case 0:
637- mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] = c;
638- break;
639- case 1:
640- mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] |= c;
641- break;
642- case 2:
643- mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] ^= c;
644- break;
645- case 3:
646- mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] &= c;
647- break;
648- }
649- }
650- }
651- }
652- break;
653-
654- case 0xff48: // drawString(opt:R31, xsiz:R32, ysiz:R33, x0:R34, y0:R35, c:R36, s.len:R37, s.p:P31)
655- checkString(r, 0x37, 0x31);
656- devFunc0006(r->ireg[0x31], r->ireg[0x32], r->ireg[0x33], r->ireg[0x34], r->ireg[0x35], loadColor(r, 0x36), r->ireg[0x37], r->preg[0x31].p, r);
657- break;
658-
659- case 0xff49: // junkApi_rand(_r, range) R31=range; _r=R30
660- // **** junkApi_rand(i, max) ****
661- // 0 <= i <= maxとなるiを返す。
662- // max==0のとき、iはSINT32全体を範囲とする乱数となる。
663- r->ireg[0x30] = randGetNextUInt32();
664- if (r->ireg[0x31] > 0){
665- r->ireg[0x30] = (r->ireg[0x30] & 0x7fffffff) % (r->ireg[0x31] + 1);
666- }
667- break;
668-
669- case 0xff4a: /* seedの指定 */
670- randStatInit(r->ireg[0x31]);
671- break;
672-
673- case 0xff4b: /* 適当なseedを提供 */
674- r->ireg[0x30] = (int)(time(NULL) ^ (long)0x55555555);
675- break;
676-
677- case 0xff4c: // drawStringDec
678- checkString(r, 0x37, 0x31);
679- len = devFunc0016(sizeof pucbuf, pucbuf, r->ireg[0x37], r->preg[0x31].p, r->ireg[0x38], (int *)r->preg[0x32].p, r);
680- devFunc0006(r->ireg[0x31], r->ireg[0x32], r->ireg[0x33], r->ireg[0x34], r->ireg[0x35], loadColor(r, 0x36), len, pucbuf, r);
681- break;
682-
683- case 0xff4d:
684- // bitblt(mod, xsiz, ysiz, xscale, yscale, x0, y0, lineskip, inv, p_buf, typ0, p_table, typ1);
685- // mod: 0x20:use_table, 0x40:inv_is_visible & typ1_is_color
686- puc = r->preg[0x31].p;
687- mod3 = r->ireg[0x31] & 3;
688- dx = r->ireg[0x34];
689- dy = r->ireg[0x35];
690- if (dy == 0){
691- dy = dx;
692- }
693- if (r->ireg[0x32] == -1) {
694- r->ireg[0x32] = mainWindow.xsize / dx; r->ireg[0x36] &= 0;
695- }
696- if (r->ireg[0x33] == -1) {
697- r->ireg[0x33] = mainWindow.ysize / dy; r->ireg[0x37] &= 0;
698- }
699- for (y = 0; y < r->ireg[0x33]; y++) {
700- y0 = y * dy + r->ireg[0x37];
701- for (x = 0; x < r->ireg[0x32]; x++) {
702- x0 = x * dx + r->ireg[0x36];
703- c = iColor1[*puc++];
704- devFunc0004(mod3, x0, y0, x0 + dx, y0 + dy, c);
705- }
706- puc += r->ireg[0x38];
707- }
708- break;
709-
710- default:
711- printf("devFunc: error: R30=%08X\n", r->ireg[0x30]);
712- exit(EXIT_FAILURE);
713-
714- }
715- return;
716-}
717-
718-
719-
1+#include "osecpu.h"
2+
3+extern unsigned char fontdata[]; // @fontdata.c
4+
5+const char *searchArg(int argc, const char **argv, const char *tag, int i)
6+{
7+ // 引数リストargvの中から、文字列tagが前方一致する引数のi番目を探し出し、
8+ // その引数の文字列の、tagに続く部分の文字へのポインタを返す。
9+ int j, l;
10+ const char *r = NULL;
11+
12+ if (tag != NULL) {
13+ l = (int)strlen(tag);
14+ for (j = 1; j < argc; j++) {
15+ if (strncmp(argv[j], tag, l) == 0) {
16+ r = argv[j] + l;
17+ if (i == 0){
18+ break;
19+ }
20+ i--;
21+ }
22+ }
23+ }
24+ /*
25+ // 未使用
26+ else {
27+ for (j = 1; j < argc; j++) {
28+ if (strchr(argv[j], ':') == NULL) {
29+ r = argv[j];
30+ if (i == 0){
31+ break;
32+ }
33+ i--;
34+ }
35+ }
36+ }
37+ */
38+ if (i != 0){
39+ r = NULL;
40+ }
41+ return r;
42+}
43+
44+void devFunc0001(int len, unsigned char *puc, HOSECPU_RuntimeEnvironment *r)
45+{
46+ // OSASK文字列の出力
47+ while (len > 0) {
48+ putOsaskChar(*puc++, r);
49+ len--;
50+ }
51+ return;
52+}
53+
54+void devFunc0006(int mod, int sx, int sy, int x, int y, int c, int len, unsigned char *puc, HOSECPU_RuntimeEnvironment *r)
55+{
56+ // drawString
57+ int xx;
58+ int yy;
59+ int i, ddx, ddy, j, ch, dx, dy;
60+
61+ if (sy == 0){
62+ sy = sx;
63+ }
64+ xx = x + sx * 8;
65+ yy = y + sy * 16;
66+ if (xx <= 0 || xx > mainWindow.xsize || yy <= 0 || yy > mainWindow.ysize){
67+ (*(r->errHndl))(r);
68+ }
69+ if (x < 0 || x >= mainWindow.xsize || y < 0 || y >= mainWindow.ysize){
70+ (*(r->errHndl))(r);
71+ }
72+
73+
74+ if ((mod & 3) == 0 && sx == 1 && sy == 1) {
75+ // メジャーケースを高速化.
76+ for (i = 0; i < len; i++) {
77+ ch = puc[i];
78+ if (0x10 <= ch && ch <= 0x1f)
79+ ch = "0123456789ABCDEF"[ch & 0x0f];
80+ for (dy = 0; dy < 16; dy++) {
81+ j = fontdata[(ch - ' ') * 16 + dy];
82+ for (dx = 0; dx < 8; dx++) {
83+ if ((j & (0x80 >> dx)) != 0){
84+ mainWindow.vram[(x + dx) + (y + dy) * mainWindow.xsize] = c;
85+ }
86+ }
87+ }
88+ x += 8;
89+ }
90+ return;
91+ }
92+ for (i = 0; i < len; i++) {
93+ ch = puc[i];
94+ if (0x10 <= ch && ch <= 0x1f)
95+ ch = "0123456789ABCDEF"[ch & 0x0f];
96+ for (dy = 0; dy < 16; dy++) {
97+ j = fontdata[(ch - ' ') * 16 + dy];
98+ for (ddy = 0; ddy < sy; ddy++) {
99+ for (dx = 0; dx < 8; dx++) {
100+ if ((j & (0x80 >> dx)) != 0) {
101+ for (ddx = 0; ddx < sx; ddx++) {
102+ switch (mod & 3) {
103+ case 0:
104+ mainWindow.vram[x + y * mainWindow.xsize] = c;
105+ break;
106+ case 1:
107+ mainWindow.vram[x + y * mainWindow.xsize] |= c;
108+ break;
109+ case 2:
110+ mainWindow.vram[x + y * mainWindow.xsize] ^= c;
111+ break;
112+ case 3:
113+ mainWindow.vram[x + y * mainWindow.xsize] &= c;
114+ break;
115+ }
116+ x++;
117+ }
118+ } else{
119+ x += sx;
120+ }
121+ }
122+ x -= sx * 8;
123+ y++;
124+ }
125+ }
126+ x += sx * 8;
127+ y -= sy * 16;
128+ }
129+ return;
130+}
131+
132+void devFunc0004(int mod, int x0, int y0, int x1, int y1, int c)
133+{
134+ // FillRect
135+ int x, y;
136+ if (mod == 0) {
137+ for (y = y0; y <= y1; y++) {
138+ for (x = x0; x <= x1; x++) {
139+ mainWindow.vram[x + y * mainWindow.xsize] = c;
140+ }
141+ }
142+ }
143+ else {
144+ for (y = y0; y <= y1; y++) {
145+ for (x = x0; x <= x1; x++) {
146+ switch (mod) {
147+ case 1:
148+ mainWindow.vram[x + y * mainWindow.xsize] |= c;
149+ break;
150+ case 2:
151+ mainWindow.vram[x + y * mainWindow.xsize] ^= c;
152+ break;
153+ case 3:
154+ mainWindow.vram[x + y * mainWindow.xsize] &= c;
155+ break;
156+ }
157+ }
158+ }
159+ }
160+ return;
161+}
162+
163+int devFunc0016(int buflen, unsigned char *buf, int plen, unsigned char *p, int qlen, int *q, HOSECPU_RuntimeEnvironment *r)
164+{
165+ // OSASK文字列への変換?
166+ int i = 0, base, j, k;
167+ char sign;
168+
169+ while (plen > 0) {
170+ if (i >= buflen){
171+ (*(r->errHndl))(r);
172+ }
173+ if (*p != 0x01) {
174+ buf[i++] = *p++;
175+ plen--;
176+ continue;
177+ }
178+ p++;
179+ plen--;
180+ if (qlen < 4){
181+ (*(r->errHndl))(r);
182+ }
183+ base = q[0];
184+ sign = 0;
185+ if (base == 0){
186+ base = 16;
187+ }
188+#if (REVISION == 1)
189+ if (base == -3){
190+ base = 10;
191+ }
192+#endif
193+ if (base == -1){
194+ base = 10;
195+ }
196+ if (base < 0 || base > 16){
197+ (*(r->errHndl))(r);
198+ }
199+ if (q[1] + i > buflen){
200+ (*(r->errHndl))(r);
201+ }
202+ j = q[3];
203+ if ((q[2] & 4) == 0) {
204+ // jは符号付き整数.
205+ if ((q[2] & 8) != 0 && j > 0){
206+ sign = '+';
207+ }
208+ if (j < 0) {
209+ sign = '-'; j *= -1;
210+ }
211+ } else{
212+ // jは符号無し整数.
213+ if ((q[2] & 8) != 0 && j != 0){
214+ sign = '+';
215+ }
216+ }
217+ for (k = q[1] - 1; k >= 0; k--) {
218+ buf[i + k] = (j % base) + 0x10;
219+ j = ((unsigned)j) / base;
220+ }
221+ k = 0;
222+ if ((q[2] & 2) == 0 && j == 0) {
223+ for (k = 0; k < q[1] - 1; k++) {
224+ if (buf[i + k] != 0x10){
225+ break;
226+ }
227+ buf[i + k] = ' ';
228+ }
229+ }
230+ if (sign != 0) {
231+ if (k > 0){
232+ k--;
233+ }
234+ buf[i + k] = sign;
235+ }
236+ if ((q[2] & 1) != 0 && buf[i] == ' ') {
237+ for (j = 0; k < q[1]; k++, j++){
238+ buf[i + j] = buf[i + k];
239+ }
240+ i += j;
241+ } else{
242+ i += q[1];
243+ }
244+ qlen -= 4;
245+ q += 4;
246+ }
247+ return i;
248+}
249+
250+void devFunc(HOSECPU_RuntimeEnvironment *r)
251+{
252+ FILE *fp;
253+ int i, c;
254+ int x, y, len, dx, dy;
255+ unsigned char *puc;
256+ unsigned char pucbuf[256];
257+
258+ //サイズを節約するためにEBPを128バイトずらしているのを元に戻す
259+ r = (HOSECPU_RuntimeEnvironment *) (((char *)r) - 128);
260+
261+ if (r->winClosed != 0){
262+ longjmp(r->setjmpEnv, 1);
263+ }
264+ if (0xff44 <= r->ireg[0x30] && r->ireg[0x30] <= 0xff48) {
265+ if (mainWindow.vram == NULL) {
266+ mainWindow.xsize = 640;
267+ mainWindow.ysize = 480;
268+ mainWindow.vram = malloc(640 * 480 * 4);
269+ drv_openWin(640, 480, (void *)mainWindow.vram, &r->winClosed);
270+ r->autoSleep = 1;
271+ for (i = 640 * 480 - 1; i >= 0; i--){
272+ mainWindow.vram[i] = 0;
273+ }
274+ }
275+ }
276+
277+ switch (r->ireg[0x30]){
278+ case 0xff00:
279+ printf("R31=%d(dec)\n", r->ireg[0x31]);
280+ break;
281+
282+ case 0xff01:
283+ // junkApi_fopenRead(_filesize, _p, arg)
284+ // args: R31=arg, R30=_filesize, P31=_p
285+ // retv: R30, P31
286+ if (r->buf0 == NULL){
287+ r->buf0 = malloc(1024 * 1024);
288+ }
289+ if (r->mainArgc <= r->ireg[0x31]) {
290+ fprintf(stderr, "devFunc: error: R30=ff01: argc error: R31=%08X\n", r->ireg[0x31]);
291+ exit(1);
292+ }
293+ fp = fopen(r->mainArgv[r->ireg[0x31]], "rb");
294+ if (fp == NULL) {
295+ fprintf(stderr, "devFunc: error: R30=ff01: fopen error: '%s'\n", r->mainArgv[r->ireg[0x31]]);
296+ exit(1);
297+ }
298+ i = (int)fread(r->buf0, 1, 1024 * 1024 - 4, fp);
299+ if (i >= 1024 * 1024 - 4 || i < 0) {
300+ fprintf(stderr, "devFunc: error: R30=ff01: fread error: '%s'\n", r->mainArgv[r->ireg[0x31]]);
301+ exit(1);
302+ }
303+ fclose(fp);
304+ r->preg[0x31].p = r->buf0;
305+ r->preg[0x31].p0 = r->buf0;
306+ r->preg[0x31].p1 = r->buf0 + i;
307+ r->preg[0x31].typ = 3; // T_UINT8
308+ r->ireg[0x30] = i;
309+ break;
310+
311+ case 0xff02:
312+ // junkApi_fopenWrite(arg, filesize, p)
313+ // args: R31=arg, R32=filesize, P31=p
314+ // retv: (none)
315+ if (r->mainArgc <= r->ireg[0x31]) {
316+ fprintf(stderr, "devFunc: error: R30=ff02: argc error: R31=%08X\n", r->ireg[0x31]);
317+ exit(1);
318+ }
319+ fp = fopen(r->mainArgv[r->ireg[0x31]], "wb");
320+ if (fp == NULL) {
321+ fprintf(stderr, "devFunc: error: R30=ff02: fopen error: '%s'\n", r->mainArgv[r->ireg[0x31]]);
322+ exit(1);
323+ }
324+ if (r->ireg[0x32] >= 1024 * 1024 || r->ireg[0x32] < 0){
325+ fprintf(stderr, "devFunc: error: R30=ff02: fwrite error: R02=%08X\n", r->ireg[0x32]);
326+ exit(1);
327+ }
328+ fwrite(r->preg[0x31].p, 1, r->ireg[0x32], fp);
329+ fclose(fp);
330+ break;
331+
332+ case 0xff03:
333+ // junkApi_allocBuf(_p)
334+ // args: P31=_p
335+ // retv: P31
336+ if (r->buf1 == NULL){
337+ r->buf1 = malloc(1024 * 1024);
338+ }
339+ r->preg[0x31].p = r->buf1;
340+ r->preg[0x31].p0 = r->buf1;
341+ r->preg[0x31].p1 = r->buf1 + 1024 * 1024;
342+ break;
343+
344+ case 0xff04:
345+ printf("P31.(p-p0)=%d(dec)\n", (int)(r->preg[0x31].p - r->preg[0x31].p0));
346+ break;
347+
348+ case 0xff05:
349+ // junkApi_writeStdout(len, p)
350+ // args: R31=len, P31=p
351+ // retv: (none)
352+ fwrite(r->preg[0x31].p, 1, r->ireg[0x31], stdout);
353+ break;
354+
355+ case 0xff06:
356+ // junkApi_exit(i)
357+ // args: R31=i
358+ // retv: (none)
359+ r->appReturnCode = r->ireg[31];
360+ longjmp(r->setjmpEnv, 1);
361+ break;
362+
363+ case 0xff07: // junkApi_putConstString0(s) DB(0xff,0x00,0x00); DB%(s,0x00);
364+ // マシになった文字列表示.OSASK文字列に対応.offにすれば通常の文字列処理もできる.現状はonのみサポート.
365+ checkString(r, 0x31, 0x31);
366+ devFunc0001(r->ireg[0x31], r->preg[0x31].p, r);
367+ break;
368+
369+ case 0xff08: // junkApi_jitc2(_rc, _p, mod, lev, di1, len, s) R31=mod; R32=lev; R33=di1; R34=len; P31=s; _rc=R30; _p=P31
370+ // JITC on JITC
371+ // R31: 言語(back-end, front-end, ...
372+ // R32: level
373+ // R33: debugInfo1
374+ checkString(r, 0x34, 0x31);
375+ if (r->ireg[0x33] < 0 || r->ireg[0x33] > 15 || r->debugInfo1 > 15){
376+ (*(r->errHndl))(r);
377+ }
378+ for (i = 0; i < r->maxLabels; i++){
379+ r->label[i].opt = 0;
380+ }
381+ puc = r->preg[0x31].p;
382+ i = jitCompiler(r->jitbuf, r->jitbuf1, puc, puc + r->ireg[0x34], puc, r->label, r->maxLabels, r->ireg[0x32], di1_serial, JITC_NOSTARTUP + 0);
383+ if (i == 0) {
384+ i = jitCompiler(r->jitbuf, r->jitbuf1, puc, puc + r->ireg[0x34], puc, r->label, r->maxLabels, r->ireg[0x32], di1_serial, JITC_NOSTARTUP + JITC_PHASE1 + 0);
385+ if (i >= 0) {
386+ r->mapDi1s[r->debugInfo1][r->ireg[0x33]] = di1_serial;
387+ di1_serial++;
388+ r->ireg[0x30] = 0;
389+ r->preg[0x31].p = r->jitbuf;
390+ r->preg[0x31].typ = 0; // TYP_CODE
391+ r->preg[0x31].p0 = r->jitbuf;
392+ r->preg[0x31].p1 = r->jitbuf + 1;
393+ //int j; for (j = 0; j < i; j++) printf("%02X ", r->jitbuf[j]); putchar('\n');
394+ r->jitbuf += i;
395+ static unsigned char ff08_ret[3] = { 0x1e, 0x3f, 0x30 };
396+ i = jitCompiler(r->jitbuf, r->jitbuf1, ff08_ret, ff08_ret + 3, puc, r->label, r->maxLabels, r->ireg[0x32], -1, JITC_NOSTARTUP + JITC_PHASE1 + 0);
397+ r->jitbuf += i;
398+ break;
399+ }
400+ }
401+ r->ireg[0x30] = -1;
402+ break;
403+
404+ case 0xff09: //putStringDec
405+ // たぶんbit7を使ったテキストはうまく処理できない(これはもはや仕様にしても問題ないかも).
406+ checkString(r, 0x31, 0x31);
407+ len = devFunc0016(sizeof pucbuf, pucbuf, r->ireg[0x31], r->preg[0x31].p, r->ireg[0x32], (int *)r->preg[0x32].p, r);
408+ devFunc0001(len, pucbuf, r);
409+ break;
410+
411+ case 0xff40: // junkApi_openWin(xsiz, ysiz) R31=xsiz; R32=ysiz;
412+ /* R31とR32でサイズを指定 */
413+ mainWindow.xsize = r->ireg[0x31];
414+ mainWindow.ysize = r->ireg[0x32];
415+ if (mainWindow.xsize <= 0 || mainWindow.ysize <= 0){
416+ (*(r->errHndl))(r);
417+ }
418+ r->preg[0x31].p = (void *)(mainWindow.vram = malloc(mainWindow.xsize * mainWindow.ysize * 4));
419+ r->preg[0x31].p0 = r->preg[0x31].p;
420+ r->preg[0x31].p1 = r->preg[0x31].p + mainWindow.xsize * mainWindow.ysize * 4;
421+ drv_openWin(r->ireg[0x31], r->ireg[0x32], r->preg[0x31].p, &r->winClosed);
422+ // drv_flshWin(r->ireg[1], r->ireg[2], 0, 0);
423+ r->autoSleep = 1;
424+ for (i = mainWindow.xsize * mainWindow.ysize - 1; i >= 0; i--){
425+ mainWindow.vram[i] = 0;
426+ }
427+ break;
428+
429+ case 0xff41: // junkApi_flushWin(xsiz, ysiz, x0, y0) R31=xsiz; R32=ysiz; R33=x0; R34=y0
430+ /* R31とR32でサイズを指定、R33とR34でx0,y0指定 */
431+ if (r->ireg[0x31] == -1) {
432+ r->ireg[0x31] = mainWindow.xsize; r->ireg[0x33] &= 0;
433+ }
434+ if (r->ireg[0x32] == -1) {
435+ r->ireg[0x32] = mainWindow.ysize; r->ireg[0x34] &= 0;
436+ }
437+ checkRect(r, 0x31);
438+ drv_flshWin(r->ireg[0x31], r->ireg[0x32], r->ireg[0x33], r->ireg[0x34]);
439+ break;
440+
441+ case 0xff42: // junkApi_sleep(opt, msec) R31=opt; R32=msec
442+ if (r->ireg[0x32] == -1) {
443+ r->autoSleep = 1;
444+ longjmp(r->setjmpEnv, 1);
445+ }
446+ if (r->ireg[0x32] < 0){
447+ (*(r->errHndl))(r);
448+ }
449+ r->autoSleep = 0;
450+ if ((r->ireg[0x31] & 1) == 0 && mainWindow.vram != NULL){
451+ drv_flshWin(mainWindow.xsize, mainWindow.ysize, 0, 0);
452+ }
453+ for (;;) {
454+ if (r->winClosed != 0){
455+ longjmp(r->setjmpEnv, 1);
456+ }
457+ drv_sleep(r->ireg[0x32]);
458+ if ((r->ireg[0x31] & 2) != 0 && keybuf_c <= 0){
459+ continue;
460+ }
461+ break;
462+ }
463+ break;
464+
465+ case 0xff43: // junkApi_inkey(_i, mod) R31=mod; _i=R30
466+ // 1:peek
467+ // 2:stdin
468+ // 4,8: ソース指定.
469+ // 16: shift, lock系を有効化.
470+ // 32: 左右のshift系を区別する.
471+ if (r->ireg[0x31] == 2) { // なぜ3にしなかったのか...
472+ r->ireg[0x30] = fgetc(stdin);
473+ if (r->ireg[0x30] == EOF){
474+ r->ireg[0x30] = -1;
475+ }
476+ break;
477+ }
478+ r->ireg[0x30] |= -1;
479+ if (keybuf_c > 0) {
480+ r->ireg[0x30] = keybuf[keybuf_r];
481+ if ((r->ireg[0x31] & 16) == 0){
482+ r->ireg[0x30] &= 0x3e3effff;
483+ }
484+ if ((r->ireg[0x31] & 32) == 0){
485+ r->ireg[0x30] |= (r->ireg[0x30] >> 8) & 0xff0000;
486+ }
487+ if ((r->ireg[0x31] & 1) != 0) {
488+ keybuf_c--;
489+ keybuf_r = (keybuf_r + 1) & (KEYBUFSIZ - 1);
490+ }
491+ }
492+ r->ireg[0x32] = r->ireg[0x33] = 0;
493+ if (r->ireg[0x30] == 4132) r->ireg[0x32]--;
494+ if (r->ireg[0x30] == 4133) r->ireg[0x33]--;
495+ if (r->ireg[0x30] == 4134) r->ireg[0x32]++;
496+ if (r->ireg[0x30] == 4135) r->ireg[0x33]++;
497+ break;
498+
499+ case 0xff44: // junkApi_drawPoint(mod, x, y, c) R31=mod; R32=x; R33=y; R34=c
500+ c = loadColor(r, 0x34);
501+ if (r->ireg[0x32] < 0 || r->ireg[0x32] >= mainWindow.xsize ||
502+ r->ireg[0x33] < 0 || r->ireg[0x33] >= mainWindow.ysize){
503+ (*(r->errHndl))(r);
504+ }
505+
506+ switch ((r->ireg[0x31] & 3)) {
507+ case 0:
508+ mainWindow.vram[r->ireg[0x32] + r->ireg[0x33] * mainWindow.xsize] = c;
509+ break;
510+ case 1:
511+ mainWindow.vram[r->ireg[0x32] + r->ireg[0x33] * mainWindow.xsize] |= c;
512+ break;
513+ case 2:
514+ mainWindow.vram[r->ireg[0x32] + r->ireg[0x33] * mainWindow.xsize] ^= c;
515+ break;
516+ case 3:
517+ mainWindow.vram[r->ireg[0x32] + r->ireg[0x33] * mainWindow.xsize] &= c;
518+ break;
519+ }
520+ break;
521+
522+ case 0xff45: // junkApi_drawLine(mod, x0, y0, x1, y1, c) DB(0xfe,0x05,0x01); DDBE(0x0003); R30=0xff45; R31=mod; R32=x0; R33=y0; R34=x1; R35=y1; R36=c
523+ //drawLine
524+ c = loadColor(r, 0x36);
525+ if (r->ireg[0x32] < 0 || r->ireg[0x32] >= mainWindow.xsize ||
526+ r->ireg[0x33] < 0 || r->ireg[0x33] >= mainWindow.ysize){
527+ (*(r->errHndl))(r);
528+ }
529+ if (r->ireg[0x34] < 0 || r->ireg[0x34] >= mainWindow.xsize ||
530+ r->ireg[0x35] < 0 || r->ireg[0x35] >= mainWindow.ysize){
531+ (*(r->errHndl))(r);
532+ }
533+ dx = r->ireg[0x34] - r->ireg[0x32];
534+ dy = r->ireg[0x35] - r->ireg[0x33];
535+ x = r->ireg[0x32] << 10;
536+ y = r->ireg[0x33] << 10;
537+ if (dx < 0){
538+ dx = -dx;
539+ }
540+ if (dy < 0){
541+ dy = -dy;
542+ }
543+ if (dx >= dy) {
544+ len = dx + 1; dx = 1024;
545+ if (r->ireg[0x32] > r->ireg[0x34]){
546+ dx *= -1;
547+ }
548+ if (r->ireg[0x33] > r->ireg[0x35]){
549+ dy *= -1;
550+ }
551+ dy = (dy << 10) / len;
552+ } else {
553+ len = dy + 1; dy = 1024;
554+ if (r->ireg[0x33] > r->ireg[0x35]){
555+ dy *= -1;
556+ }
557+ if (r->ireg[0x32] > r->ireg[0x34]){
558+ dx *= -1;
559+ }
560+ dx = (dx << 10) / len;
561+ }
562+ if ((r->ireg[0x31] & 3) == 0) {
563+ for (i = 0; i < len; i++) {
564+ mainWindow.vram[(x >> 10) + (y >> 10) * mainWindow.xsize] = c;
565+ x += dx;
566+ y += dy;
567+ }
568+ break;
569+ }
570+ for (i = 0; i < len; i++) {
571+ // if ((r->ireg[0x31] & 3) == 0) vram[(x >> 10) + (y >> 10) * v_xsiz] = c;
572+ switch ((r->ireg[0x31] & 3)) {
573+ case 1:
574+ mainWindow.vram[(x >> 10) + (y >> 10) * mainWindow.xsize] |= c;
575+ break;
576+ case 2:
577+ mainWindow.vram[(x >> 10) + (y >> 10) * mainWindow.xsize] ^= c;
578+ break;
579+ case 3:
580+ mainWindow.vram[(x >> 10) + (y >> 10) * mainWindow.xsize] &= c;
581+ break;
582+ default:
583+ break;
584+ }
585+ x += dx;
586+ y += dy;
587+ }
588+ break;
589+
590+ case 0xff46: // fillRect(opt:R31, xsiz:R32, ysiz:R33, x0:R34, y0:R35, c:R36)
591+ c = loadColor(r, 0x36);
592+ if (r->ireg[0x32] == -1) { r->ireg[0x32] = mainWindow.xsize; r->ireg[0x34] &= 0; }
593+ if (r->ireg[0x33] == -1) { r->ireg[0x33] = mainWindow.ysize; r->ireg[0x35] &= 0; }
594+ checkRect(r, 0x32);
595+ int mod3 = r->ireg[0x31] & 3, x0 = r->ireg[0x34], y0 = r->ireg[0x35], x1 = r->ireg[0x34] + r->ireg[0x32] - 1, y1 = r->ireg[0x35] + r->ireg[0x33] - 1;
596+ if ((r->ireg[0x31] & 0x20) == 0) {
597+ devFunc0004(mod3, x0, y0, x1, y1, c);
598+ } else { // drawRect
599+ devFunc0004(mod3, x0, y0, x1, y0, c);
600+ devFunc0004(mod3, x0, y1, x1, y1, c);
601+ devFunc0004(mod3, x0, y0, x0, y1, c);
602+ devFunc0004(mod3, x1, y0, x1, y1, c);
603+ }
604+ break;
605+
606+ case 0xff47: // fillOval(opt:R31, xsiz:R32, ysiz:R33, x0:R34, y0:R35, c:R36)
607+ // これの計算精度はアーキテクチャに依存する.
608+ c = loadColor(r, 0x36);
609+ if (r->ireg[0x32] == -1) {
610+ r->ireg[0x32] = mainWindow.xsize; r->ireg[0x34] &= 0;
611+ }
612+ if (r->ireg[0x33] == -1) {
613+ r->ireg[0x33] = mainWindow.ysize; r->ireg[0x35] &= 0;
614+ }
615+ checkRect(r, 0x32);
616+ double dcx = 0.5 * (r->ireg[0x32] - 1);
617+ double dcy = 0.5 * (r->ireg[0x33] - 1);
618+ double dcxy = (dcx + 0.5) * (dcy + 0.5) - 0.1;
619+ dcxy *= dcxy;
620+ mod3 = r->ireg[0x31] & 3;
621+ x1 = r->ireg[0x32];
622+ y1 = r->ireg[0x33];
623+ if (mod3 == 0 && (r->ireg[0x31] & 0x20) == 0) {
624+ for (y = 0; y < y1; y++) {
625+ double dty = (y - dcy) * dcx;
626+ for (x = 0; x < x1; x++) {
627+ double dtx = (x - dcx) * dcy;
628+ if (dtx * dtx + dty * dty > dcxy){
629+ continue;
630+ }
631+ mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] = c;
632+ }
633+ }
634+ } else {
635+#define DRAWOVALPARAM 1
636+ double dcx1 = 0.5 * (r->ireg[0x32] - (1 + DRAWOVALPARAM * 2));
637+ double dcy1 = 0.5 * (r->ireg[0x33] - (1 + DRAWOVALPARAM * 2));
638+ double dcxy1 = (dcx1 + 0.5) * (dcy1 + 0.5) - 0.1;
639+ dcxy1 *= dcxy1;
640+ for (y = 0; y < y1; y++) {
641+ double dty = (y - dcy) * dcx;
642+ double dty1 = (y - dcy) * dcx1;
643+ for (x = 0; x < x1; x++) {
644+ double dtx = (x - dcx) * dcy;
645+ double dtx1 = (x - dcx) * dcy1;
646+ if (dtx * dtx + dty * dty > dcxy){
647+ continue;
648+ }
649+ if (DRAWOVALPARAM <= x && x < x1 - DRAWOVALPARAM && DRAWOVALPARAM <= y && y < y1 - DRAWOVALPARAM) {
650+ if (dtx1 * dtx1 + dty1 * dty1 < dcxy1){
651+ continue;
652+ }
653+ }
654+ switch (mod3) {
655+ case 0:
656+ mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] = c;
657+ break;
658+ case 1:
659+ mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] |= c;
660+ break;
661+ case 2:
662+ mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] ^= c;
663+ break;
664+ case 3:
665+ mainWindow.vram[(x + r->ireg[0x34]) + (y + r->ireg[0x35]) * mainWindow.xsize] &= c;
666+ break;
667+ }
668+ }
669+ }
670+ }
671+ break;
672+
673+ case 0xff48: // drawString(opt:R31, xsiz:R32, ysiz:R33, x0:R34, y0:R35, c:R36, s.len:R37, s.p:P31)
674+ checkString(r, 0x37, 0x31);
675+ devFunc0006(r->ireg[0x31], r->ireg[0x32], r->ireg[0x33], r->ireg[0x34], r->ireg[0x35], loadColor(r, 0x36), r->ireg[0x37], r->preg[0x31].p, r);
676+ break;
677+
678+ case 0xff49: // junkApi_rand(_r, range) R31=range; _r=R30
679+ // **** junkApi_rand(i, max) ****
680+ // 0 <= i <= maxとなるiを返す。
681+ // max==0のとき、iはSINT32全体を範囲とする乱数となる。
682+ r->ireg[0x30] = randGetNextUInt32();
683+ if (r->ireg[0x31] > 0){
684+ r->ireg[0x30] = (r->ireg[0x30] & 0x7fffffff) % (r->ireg[0x31] + 1);
685+ }
686+ break;
687+
688+ case 0xff4a: /* seedの指定 */
689+ randStatInit(r->ireg[0x31]);
690+ break;
691+
692+ case 0xff4b: /* 適当なseedを提供 */
693+ r->ireg[0x30] = (int)(time(NULL) ^ (long)0x55555555);
694+ break;
695+
696+ case 0xff4c: // drawStringDec
697+ checkString(r, 0x37, 0x31);
698+ len = devFunc0016(sizeof pucbuf, pucbuf, r->ireg[0x37], r->preg[0x31].p, r->ireg[0x38], (int *)r->preg[0x32].p, r);
699+ devFunc0006(r->ireg[0x31], r->ireg[0x32], r->ireg[0x33], r->ireg[0x34], r->ireg[0x35], loadColor(r, 0x36), len, pucbuf, r);
700+ break;
701+
702+ case 0xff4d:
703+ // bitblt(mod, xsiz, ysiz, xscale, yscale, x0, y0, lineskip, inv, p_buf, typ0, p_table, typ1);
704+ // mod: 0x20:use_table, 0x40:inv_is_visible & typ1_is_color
705+ puc = r->preg[0x31].p;
706+ mod3 = r->ireg[0x31] & 3;
707+ dx = r->ireg[0x34];
708+ dy = r->ireg[0x35];
709+ if (dy == 0){
710+ dy = dx;
711+ }
712+ if (r->ireg[0x32] == -1) {
713+ r->ireg[0x32] = mainWindow.xsize / dx; r->ireg[0x36] &= 0;
714+ }
715+ if (r->ireg[0x33] == -1) {
716+ r->ireg[0x33] = mainWindow.ysize / dy; r->ireg[0x37] &= 0;
717+ }
718+ for (y = 0; y < r->ireg[0x33]; y++) {
719+ y0 = y * dy + r->ireg[0x37];
720+ for (x = 0; x < r->ireg[0x32]; x++) {
721+ x0 = x * dx + r->ireg[0x36];
722+ c = iColor1[*puc++];
723+ devFunc0004(mod3, x0, y0, x0 + dx, y0 + dy, c);
724+ }
725+ puc += r->ireg[0x38];
726+ }
727+ break;
728+
729+ default:
730+ printf("devFunc: error: R30=%08X\n", r->ireg[0x30]);
731+ exit(EXIT_FAILURE);
732+
733+ }
734+ return;
735+}
736+
737+
738+
--- a/main.c
+++ b/main.c
@@ -1,343 +1,342 @@
1-#include "osecpu.h"
2-
3-int *keybuf, keybuf_r, keybuf_w, keybuf_c;
4-HOSECPU_Device_Window mainWindow;
5-//デバッグ用。プログラム中の随所で加算される変数
6-int di1_serial;
7-
8-
9-
10-unsigned char *Init_LoadSysLib(char argv0[], unsigned char *tmpWorkMemory);
11-void LoadAppBin(HOSECPU_RuntimeEnvironment *env);
12-
13-void putKeybuf(int i)
14-{
15- if (keybuf_c < KEYBUFSIZ) {
16- keybuf[keybuf_w] = i;
17- keybuf_c++;
18- keybuf_w = (keybuf_w + 1) & (KEYBUFSIZ - 1);
19- }
20- return;
21-}
22-
23-int HeavyOSECPUMain(int argc, char **argv)
24-{
25- FILE *fp;
26- unsigned char *jitbuf, *sysjit00, *sysjit;
27- unsigned char *systmp0, *systmp1, *systmp2;
28- unsigned char *opTbl;
29- HOSECPU_LabelListTag *label;
30- int tmpsiz, i;
31- jmp_buf setjmpEnv;
32- double tm0, tm1, tm2;
33- HOSECPU_PointerControlTag *ptrCtrl;
34- unsigned char *syslib;
35- int argDebug = 0, stacksiz = 1;
36- const char *cp;
37- HOSECPU_RuntimeEnvironment env;
38- void(*jitfunc)(char *);
39- unsigned char *jp;
40-
41- //Initialize mainWindow
42- mainWindow.vram = NULL;
43- mainWindow.xsize = 0;
44- mainWindow.ysize = 0;
45- di1_serial = 0;
46-
47- //実行環境初期化
48- env.mainArgc = argc;
49- env.mainArgv = (const char **)argv;
50- env.appBin = malloc(APPSIZ1);
51- env.executionLevel = JITC_LV_SLOWEST;
52- jitbuf = mallocRWE(1024 * 1024); /* とりあえず1MBで */
53- //unsigned char *sysjit0 = mallocRWE(SJITSIZ1), *sysjit1 = sysjit0, *sysjit00 = sysjit0;
54- // syslib.oseのjitc結果を格納する領域を確保。
55- sysjit00 = mallocRWE(SJITSIZ1);
56- sysjit = sysjit00;
57- // 現在の、jitc結果を格納するメモリへの書き込み位置のアドレス
58- // sysjit: 現在のjitc書き込み位置
59- // sysjit00: jitc結果の先頭
60- //ワークメモリを三つくらいもらう
61- systmp0 = malloc(SYSTMP0SIZ); /* syslibのjitc用 */
62- systmp1 = malloc(SYSTMP1SIZ);
63- systmp2 = malloc(1024 * 1024);
64-
65- opTbl = malloc(256);
66- label = malloc(JITC_MAXLABELS * sizeof (HOSECPU_LabelListTag));
67- keybuf = malloc(KEYBUFSIZ * sizeof (int));
68- keybuf_r = keybuf_w = keybuf_c = 0;
69- ptrCtrl = malloc(PTRCTRLSIZ * sizeof (HOSECPU_PointerControlTag));
70-
71- randStatInit((unsigned int)time(NULL));
72- for (i = 0; i < PTRCTRLSIZ; i++) {
73- ptrCtrl[i].liveSign = 0;
74- ptrCtrl[i].size = -1;
75- }
76- ptrCtrl[0].size = -2;
77-
78- /* syslibの読み込み */
79- syslib = Init_LoadSysLib(argv[0], systmp0);
80-
81- sysjit = jitCompInit(sysjit);
82- sysjit00 = sysjit;
83- // labelはjitc0()内で初期化される。
84- i = jitc0(&sysjit, sysjit00 + SJITSIZ1, syslib + 32, syslib + SYSLIBSIZ1, JITC_LV_SLOWEST+9, label);
85- if (i != 0){
86- fputs("syslib-file JITC error.\n", stderr);
87- return 1;
88- }
89-
90- // エラー時にデバッグ用に表示する変数を加算
91- di1_serial++;
92-
93- /* アプリバイナリの読み込み */
94- LoadAppBin(&env);
95-
96- /* クロック初期化 */
97- tm0 = clock() / (double)CLOCKS_PER_SEC;
98-
99- if (env.appBin[2] == 0xf0) {
100- // tek5圧縮がかかっている
101-#if (USE_TEK5 != 0)
102- env.appSize1 = tek5Decomp(env.appBin + 2, env.appBin + env.appSize0, systmp0) + 2;
103-#else
104- env.appSize1 = -9;
105-#endif
106- if (env.appSize1 < 0) {
107- fputs("unsupported-format(tek5)\n", stderr);
108- return 1;
109- }
110- }
111- //デバッグモード指定
112- cp = searchArg(argc, (const char **)argv, "debug:", 0);
113- if (cp != NULL){
114- argDebug = *cp - '0';
115- }
116- //スタックサイズ指定(MiB単位)
117- cp = searchArg(argc, (const char **)argv, "stack:", 0);
118- if (cp != NULL){
119- stacksiz = strtol(cp, NULL, 0);
120- }
121-
122- // jitbufは先頭。 jpは現在位置
123- jp = jitbuf; /* JIT-pointer */
124-
125- /* フロントエンドコードをバックエンドコードに変換する */
126- if ((env.appBin[2] & 0xf0) != 0) { // 3バイト目が00なら処理しない
127- systmp0[0] = env.appBin[0];
128- systmp0[1] = env.appBin[1];
129- env.preg[2].p = systmp0 + 2;
130- env.preg[3].p = systmp0 + SYSTMP0SIZ;
131- env.preg[4].p = env.appBin + 2;
132- env.preg[5].p = env.appBin + env.appSize1;
133- env.preg[6].p = systmp1;
134- env.preg[7].p = systmp1 + SYSTMP1SIZ;
135- env.preg[10].p = systmp2;
136- int pxxFlag[64], typLabel[4096];
137- env.preg[0x0b].p = (void *)pxxFlag;
138- env.preg[0x0c].p = (void *)typLabel;
139- env.preg[0x0d].p = opTbl;
140- jitfunc = (void *)sysjit00;
141- (*jitfunc)(((char *)&env) + 128); /* サイズを節約するためにEBPを128バイトずらす */
142- if (env.ireg[0] != 0) {
143- jp = env.preg[2].p - 1;
144- fprintf(stderr, "unpack error: %02X (at %06X) (R00=%d)\n", *jp, jp - systmp0, env.ireg[0]);
145- if ((argDebug & 2) != 0) {
146- fp = fopen("debug2.bin", "wb");
147- fwrite(systmp0, 1, jp - systmp0 + 16, fp);
148- fclose(fp);
149- }
150- exit(1);
151- }
152- tmpsiz = env.preg[2].p - systmp0;
153- } else{
154- memcpy(systmp0, env.appBin, env.appSize1);
155- tmpsiz = env.appSize1;
156- }
157-
158- if ((argDebug & 2) != 0) {
159- /*変換後のバックエンドコードをファイルへ保存*/
160- fp = fopen("debug2.bin", "wb");
161- fwrite(systmp0, 1, tmpsiz, fp);
162- fclose(fp);
163- }
164-
165- //JITコンパイル
166- i = jitc0(&jp, jitbuf + 1024 * 1024, systmp0, systmp0 + tmpsiz, env.executionLevel, label);
167- if (i == 1){
168- fputs("app-file header error.\n", stderr);
169- return 1;
170- }
171- if (i != 0){
172- return 1;
173- }
174- di1_serial++;
175-
176- int appsiz2 = jp - jitbuf;
177-
178- unsigned char *p28 = jp;
179- jp = jitCompCallFunc(jp, &devFunc);
180-
181- tm1 = clock() / (double)CLOCKS_PER_SEC;
182-
183- /* レジスタ初期化 */
184- for (i = 0; i < 64; i++){
185- env.ireg[i] = 0;
186- }
187- for (i = 0; i < 64; i++) {
188- env.preg[i].p = NULL;
189- env.preg[i].typ = -1;
190- env.preg[i].p0 = NULL;
191- env.preg[i].p1 = NULL;
192- }
193-
194- env.buf0 = env.buf1 = NULL;
195-
196- // p28にapiをコールするアドレスを設定
197- env.preg[0x28].p = p28; // p28には、devFuncをコールするコードが書かれている
198- env.preg[0x28].typ = 0; // TYP_CODE
199- env.preg[0x28].p0 = p28; // アドレス演算できる範囲を制限
200- env.preg[0x28].p1 = p28 + 1; // アドレス演算できる範囲を制限
201-
202- //env.preg[0x00].p = malloc(1024 * 1024) + (1024 * 1024 - 32);
203- env.junkStack = malloc(stacksiz << 20);
204- env.junkStack1 = env.junkStack + (stacksiz << 20);
205- env.winClosed = 0;
206- env.autoSleep = 0;
207- env.setjmpEnv = &setjmpEnv;
208- env.lastConsoleChar = '\n';
209-
210- env.label = label;
211- env.maxLabels = JITC_MAXLABELS;
212- env.jitbuf = jp;
213- env.jitbuf1 = jitbuf + 1024 * 1024;
214- env.errHndl = &errorHandler;
215-
216- env.dbgr = 0;
217- if (searchArg(argc, (const char **)argv, "dbgr:1", 0) != NULL){
218- env.dbgr = 1;
219- }
220-
221- if ((argDebug & 1) != 0) {
222- fp = fopen("debug1.bin", "wb");
223- fwrite(jitbuf, 1, jp - jitbuf, fp);
224- fclose(fp);
225- }
226-
227- /* JITコード実行 */
228- jitfunc = (void *)jitbuf;
229- if (setjmp(setjmpEnv) == 0){
230- (*jitfunc)(((char *)&env) + 128); /* サイズを節約するためにEBPを128バイトずらす */
231- }
232- if (env.autoSleep != 0) {
233- if (mainWindow.vram != NULL){
234- drv_flshWin(mainWindow.xsize, mainWindow.ysize, 0, 0);
235- }
236- while (env.winClosed == 0){
237- drv_sleep(100);
238- }
239- }
240- if (env.lastConsoleChar != '\n'){
241- putchar('\n');
242- }
243-
244- tm2 = clock() / (double)CLOCKS_PER_SEC;
245-
246- /* 実行結果確認のためのレジスタダンプ */
247- if (searchArg(argc, (const char **)argv, "verbose:1", 0) != NULL) {
248- printf("time: JITC=%.3f[sec], exec=%.3f[sec]\n", tm1 - tm0, tm2 - tm1);
249- printf("size: OSECPU=%d, decomp=%d, tmp=%d, native=%d\n", env.appSize0, env.appSize1, tmpsiz, appsiz2);
250- printf("result:\n");
251- printf("R00:0x%08X R01:0x%08X R02:0x%08X R03:0x%08X\n", env.ireg[0], env.ireg[1], env.ireg[2], env.ireg[3]);
252- }
253-#if (USE_DEBUGGER != 0)
254- dbgrMain(&env);
255-#endif
256- return 0;
257-}
258-
259-unsigned char *Init_LoadSysLib(char argv0[], unsigned char *tmpWorkMemory)
260-{
261- unsigned char *syslib;
262- FILE *fp;
263- unsigned char *up;
264- int appsize;
265-
266- /* syslibの読み込み */
267- syslib = malloc(SYSLIBSIZ1);
268- fp = fopen(SYSLIB_OSE, "rb");
269- if (fp == NULL) {
270- syslib[0] = '/';
271- strcpy((char *)syslib + 1, argv0);
272- up = syslib + 1;
273- while (*up != '\0'){
274- up++;
275- }
276- while (*up != '/' && *up != 0x5c){
277- up--;
278- }
279- up++;
280- strcpy((char *)up, SYSLIB_OSE);
281- fp = fopen((char *)syslib + 1, "rb");
282- }
283- if (fp == NULL) {
284- fputs("syslib-file fopen error.\n", stderr);
285- exit(EXIT_FAILURE);
286- }
287- appsize = fread(syslib, 1, SYSLIBSIZ1 - 4, fp);
288- fclose(fp);
289- if (appsize >= SYSLIBSIZ1 - 4) {
290- fputs("syslib-file too large.\n", stderr);
291- exit(EXIT_FAILURE);
292- }
293- if (syslib[0] == 0x05 && syslib[1] == 0xc1) {
294- // maklib のライブラリ形式である。
295- memcpy(tmpWorkMemory, syslib, appsize);
296- ComLib_main(tmpWorkMemory + 2, syslib + 2);
297- syslib[0] = 0x05;
298- syslib[1] = 0x1b;
299- }
300-
301- fp = fopen("syslib_dbg.ose", "wb");
302- fwrite(syslib, 1, SYSLIBSIZ1, fp);
303- fclose(fp);
304- return syslib;
305-}
306-
307-void LoadAppBin(HOSECPU_RuntimeEnvironment *env)
308-{
309- FILE *fp;
310- const char *fileName;
311- /* アプリバイナリの読み込み */
312- if (env->mainArgc <= 1) {
313- //アプリ名未指定なので何事もなく終了
314- exit(EXIT_SUCCESS);
315- }
316- fileName = env->mainArgv[1];
317- //アプリ名先頭に:n:をつけることでレベルnでの実行が可能?
318- if (fileName[0] == ':' && fileName[2] == ':') {
319- env->executionLevel = fileName[1] - '0';
320- if (env->executionLevel < 0 || env->executionLevel > 9){
321- env->executionLevel = JITC_LV_SLOWEST;
322- }
323- fileName += 3;
324- }
325-
326- fp = fopen(fileName, "rb");
327- if (fp == NULL) {
328- fputs("app-file load error.\n", stderr);
329- exit(EXIT_FAILURE);
330- }
331- env->appSize0 = fread(env->appBin, 1, APPSIZ1 - 4, fp);
332- env->appSize1 = env->appSize0;
333- fclose(fp);
334-
335- if (env->appSize0 >= APPSIZ1 - 4) {
336- fputs("app-file too large.\n", stderr);
337- exit(EXIT_FAILURE);
338- }
339- if (env->appSize0 < 3) {
340- fputs("app-file header error.\n", stderr);
341- exit(EXIT_FAILURE);
342- }
343-}
1+#include "osecpu.h"
2+
3+int *keybuf, keybuf_r, keybuf_w, keybuf_c;
4+HOSECPU_Device_Window mainWindow;
5+//デバッグ用。プログラム中の随所で加算される変数
6+int di1_serial;
7+
8+
9+
10+unsigned char *Init_LoadSysLib(char argv0[], unsigned char *tmpWorkMemory);
11+void LoadAppBin(HOSECPU_RuntimeEnvironment *env);
12+
13+void putKeybuf(int i)
14+{
15+ if (keybuf_c < KEYBUFSIZ) {
16+ keybuf[keybuf_w] = i;
17+ keybuf_c++;
18+ keybuf_w = (keybuf_w + 1) & (KEYBUFSIZ - 1);
19+ }
20+ return;
21+}
22+
23+int HeavyOSECPUMain(int argc, char **argv)
24+{
25+ FILE *fp;
26+ unsigned char *jitbuf, *sysjit00, *sysjit;
27+ unsigned char *systmp0, *systmp1, *systmp2;
28+ unsigned char *opTbl;
29+ HOSECPU_LabelListTag *label;
30+ int tmpsiz, i;
31+ double tm0, tm1, tm2;
32+ HOSECPU_PointerControlTag *ptrCtrl;
33+ unsigned char *syslib;
34+ int argDebug = 0, stacksiz = 1;
35+ const char *cp;
36+ HOSECPU_RuntimeEnvironment env;
37+ void(*jitfunc)(char *);
38+ unsigned char *jp;
39+
40+ //Initialize mainWindow
41+ mainWindow.vram = NULL;
42+ mainWindow.xsize = 0;
43+ mainWindow.ysize = 0;
44+ di1_serial = 0;
45+
46+ //実行環境初期化
47+ env.mainArgc = argc;
48+ env.mainArgv = (const char **)argv;
49+ env.appBin = malloc(APPSIZ1);
50+ env.executionLevel = JITC_LV_SLOWEST;
51+ jitbuf = mallocRWE(1024 * 1024); /* とりあえず1MBで */
52+ //unsigned char *sysjit0 = mallocRWE(SJITSIZ1), *sysjit1 = sysjit0, *sysjit00 = sysjit0;
53+ // syslib.oseのjitc結果を格納する領域を確保。
54+ sysjit00 = mallocRWE(SJITSIZ1);
55+ sysjit = sysjit00;
56+ // 現在の、jitc結果を格納するメモリへの書き込み位置のアドレス
57+ // sysjit: 現在のjitc書き込み位置
58+ // sysjit00: jitc結果の先頭
59+ //ワークメモリを三つくらいもらう
60+ systmp0 = malloc(SYSTMP0SIZ); /* syslibのjitc用 */
61+ systmp1 = malloc(SYSTMP1SIZ);
62+ systmp2 = malloc(1024 * 1024);
63+
64+ opTbl = malloc(256);
65+ label = malloc(JITC_MAXLABELS * sizeof (HOSECPU_LabelListTag));
66+ keybuf = malloc(KEYBUFSIZ * sizeof (int));
67+ keybuf_r = keybuf_w = keybuf_c = 0;
68+ ptrCtrl = malloc(PTRCTRLSIZ * sizeof (HOSECPU_PointerControlTag));
69+
70+ randStatInit((unsigned int)time(NULL));
71+ for (i = 0; i < PTRCTRLSIZ; i++) {
72+ ptrCtrl[i].liveSign = 0;
73+ ptrCtrl[i].size = -1;
74+ }
75+ ptrCtrl[0].size = -2;
76+
77+ /* syslibの読み込み */
78+ syslib = Init_LoadSysLib(argv[0], systmp0);
79+
80+ sysjit = jitCompInit(sysjit);
81+ sysjit00 = sysjit;
82+ // labelはjitc0()内で初期化される。
83+ i = jitc0(&sysjit, sysjit00 + SJITSIZ1, syslib + 32, syslib + SYSLIBSIZ1, JITC_LV_SLOWEST+9, label);
84+ if (i != 0){
85+ fputs("syslib-file JITC error.\n", stderr);
86+ return 1;
87+ }
88+
89+ // エラー時にデバッグ用に表示する変数を加算
90+ di1_serial++;
91+
92+ /* アプリバイナリの読み込み */
93+ LoadAppBin(&env);
94+
95+ /* クロック初期化 */
96+ tm0 = clock() / (double)CLOCKS_PER_SEC;
97+
98+ if (env.appBin[2] == 0xf0) {
99+ // tek5圧縮がかかっている
100+#if (USE_TEK5 != 0)
101+ env.appSize1 = tek5Decomp(env.appBin + 2, env.appBin + env.appSize0, systmp0) + 2;
102+#else
103+ env.appSize1 = -9;
104+#endif
105+ if (env.appSize1 < 0) {
106+ fputs("unsupported-format(tek5)\n", stderr);
107+ return 1;
108+ }
109+ }
110+ //デバッグモード指定
111+ cp = searchArg(argc, (const char **)argv, "debug:", 0);
112+ if (cp != NULL){
113+ argDebug = *cp - '0';
114+ }
115+ //スタックサイズ指定(MiB単位)
116+ cp = searchArg(argc, (const char **)argv, "stack:", 0);
117+ if (cp != NULL){
118+ stacksiz = strtol(cp, NULL, 0);
119+ }
120+
121+ // jitbufは先頭。 jpは現在位置
122+ jp = jitbuf; /* JIT-pointer */
123+
124+ /* フロントエンドコードをバックエンドコードに変換する */
125+ if ((env.appBin[2] & 0xf0) != 0) { // 3バイト目が00なら処理しない
126+ systmp0[0] = env.appBin[0];
127+ systmp0[1] = env.appBin[1];
128+ env.preg[2].p = systmp0 + 2;
129+ env.preg[3].p = systmp0 + SYSTMP0SIZ;
130+ env.preg[4].p = env.appBin + 2;
131+ env.preg[5].p = env.appBin + env.appSize1;
132+ env.preg[6].p = systmp1;
133+ env.preg[7].p = systmp1 + SYSTMP1SIZ;
134+ env.preg[10].p = systmp2;
135+ int pxxFlag[64], typLabel[4096];
136+ env.preg[0x0b].p = (void *)pxxFlag;
137+ env.preg[0x0c].p = (void *)typLabel;
138+ env.preg[0x0d].p = opTbl;
139+ jitfunc = (void *)sysjit00;
140+ (*jitfunc)(((char *)&env) + 128); /* サイズを節約するためにEBPを128バイトずらす */
141+ if (env.ireg[0] != 0) {
142+ jp = env.preg[2].p - 1;
143+ fprintf(stderr, "unpack error: %02X (at %06X) (R00=%d)\n", *jp, jp - systmp0, env.ireg[0]);
144+ if ((argDebug & 2) != 0) {
145+ fp = fopen("debug2.bin", "wb");
146+ fwrite(systmp0, 1, jp - systmp0 + 16, fp);
147+ fclose(fp);
148+ }
149+ exit(1);
150+ }
151+ tmpsiz = env.preg[2].p - systmp0;
152+ } else{
153+ memcpy(systmp0, env.appBin, env.appSize1);
154+ tmpsiz = env.appSize1;
155+ }
156+
157+ if ((argDebug & 2) != 0) {
158+ /*変換後のバックエンドコードをファイルへ保存*/
159+ fp = fopen("debug2.bin", "wb");
160+ fwrite(systmp0, 1, tmpsiz, fp);
161+ fclose(fp);
162+ }
163+
164+ //JITコンパイル
165+ i = jitc0(&jp, jitbuf + 1024 * 1024, systmp0, systmp0 + tmpsiz, env.executionLevel, label);
166+ if (i == 1){
167+ fputs("app-file header error.\n", stderr);
168+ return 1;
169+ }
170+ if (i != 0){
171+ return 1;
172+ }
173+ di1_serial++;
174+
175+ int appsiz2 = jp - jitbuf;
176+
177+ unsigned char *p28 = jp;
178+ jp = jitCompCallFunc(jp, &devFunc);
179+
180+ tm1 = clock() / (double)CLOCKS_PER_SEC;
181+
182+ /* レジスタ初期化 */
183+ for (i = 0; i < 64; i++){
184+ env.ireg[i] = 0;
185+ }
186+ for (i = 0; i < 64; i++) {
187+ env.preg[i].p = NULL;
188+ env.preg[i].typ = -1;
189+ env.preg[i].p0 = NULL;
190+ env.preg[i].p1 = NULL;
191+ }
192+
193+ env.buf0 = env.buf1 = NULL;
194+
195+ // p28にapiをコールするアドレスを設定
196+ env.preg[0x28].p = p28; // p28には、devFuncをコールするコードが書かれている
197+ env.preg[0x28].typ = 0; // TYP_CODE
198+ env.preg[0x28].p0 = p28; // アドレス演算できる範囲を制限
199+ env.preg[0x28].p1 = p28 + 1; // アドレス演算できる範囲を制限
200+
201+ //env.preg[0x00].p = malloc(1024 * 1024) + (1024 * 1024 - 32);
202+ env.junkStack = malloc(stacksiz << 20);
203+ env.junkStack1 = env.junkStack + (stacksiz << 20);
204+ env.winClosed = 0;
205+ env.autoSleep = 0;
206+ env.lastConsoleChar = '\n';
207+
208+ env.label = label;
209+ env.maxLabels = JITC_MAXLABELS;
210+ env.jitbuf = jp;
211+ env.jitbuf1 = jitbuf + 1024 * 1024;
212+ env.errHndl = &errorHandler;
213+ env.appReturnCode = 0;
214+
215+ env.dbgr = 0;
216+ if (searchArg(argc, (const char **)argv, "dbgr:1", 0) != NULL){
217+ env.dbgr = 1;
218+ }
219+
220+ if ((argDebug & 1) != 0) {
221+ fp = fopen("debug1.bin", "wb");
222+ fwrite(jitbuf, 1, jp - jitbuf, fp);
223+ fclose(fp);
224+ }
225+
226+ /* JITコード実行 */
227+ jitfunc = (void *)jitbuf;
228+ if (setjmp(env.setjmpEnv) == 0){
229+ (*jitfunc)(((char *)&env) + 128); /* サイズを節約するためにEBPを128バイトずらす */
230+ }
231+ if (env.autoSleep != 0) {
232+ if (mainWindow.vram != NULL){
233+ drv_flshWin(mainWindow.xsize, mainWindow.ysize, 0, 0);
234+ }
235+ while (env.winClosed == 0){
236+ drv_sleep(100);
237+ }
238+ }
239+ if (env.lastConsoleChar != '\n'){
240+ putchar('\n');
241+ }
242+
243+ tm2 = clock() / (double)CLOCKS_PER_SEC;
244+
245+ /* 実行結果確認のためのレジスタダンプ */
246+ if (searchArg(argc, (const char **)argv, "verbose:1", 0) != NULL) {
247+ printf("time: JITC=%.3f[sec], exec=%.3f[sec]\n", tm1 - tm0, tm2 - tm1);
248+ printf("size: OSECPU=%d, decomp=%d, tmp=%d, native=%d\n", env.appSize0, env.appSize1, tmpsiz, appsiz2);
249+ printf("result:\n");
250+ printf("R00:0x%08X R01:0x%08X R02:0x%08X R03:0x%08X\n", env.ireg[0], env.ireg[1], env.ireg[2], env.ireg[3]);
251+ }
252+#if (USE_DEBUGGER != 0)
253+ dbgrMain(&env);
254+#endif
255+ return env.appReturnCode;
256+}
257+
258+unsigned char *Init_LoadSysLib(char argv0[], unsigned char *tmpWorkMemory)
259+{
260+ unsigned char *syslib;
261+ FILE *fp;
262+ unsigned char *up;
263+ int appsize;
264+
265+ /* syslibの読み込み */
266+ syslib = malloc(SYSLIBSIZ1);
267+ fp = fopen(SYSLIB_OSE, "rb");
268+ if (fp == NULL) {
269+ syslib[0] = '/';
270+ strcpy((char *)syslib + 1, argv0);
271+ up = syslib + 1;
272+ while (*up != '\0'){
273+ up++;
274+ }
275+ while (*up != '/' && *up != 0x5c){
276+ up--;
277+ }
278+ up++;
279+ strcpy((char *)up, SYSLIB_OSE);
280+ fp = fopen((char *)syslib + 1, "rb");
281+ }
282+ if (fp == NULL) {
283+ fputs("syslib-file fopen error.\n", stderr);
284+ exit(EXIT_FAILURE);
285+ }
286+ appsize = fread(syslib, 1, SYSLIBSIZ1 - 4, fp);
287+ fclose(fp);
288+ if (appsize >= SYSLIBSIZ1 - 4) {
289+ fputs("syslib-file too large.\n", stderr);
290+ exit(EXIT_FAILURE);
291+ }
292+ if (syslib[0] == 0x05 && syslib[1] == 0xc1) {
293+ // maklib のライブラリ形式である。
294+ memcpy(tmpWorkMemory, syslib, appsize);
295+ ComLib_main(tmpWorkMemory + 2, syslib + 2);
296+ syslib[0] = 0x05;
297+ syslib[1] = 0x1b;
298+ }
299+
300+ fp = fopen("syslib_dbg.ose", "wb");
301+ fwrite(syslib, 1, SYSLIBSIZ1, fp);
302+ fclose(fp);
303+ return syslib;
304+}
305+
306+void LoadAppBin(HOSECPU_RuntimeEnvironment *env)
307+{
308+ FILE *fp;
309+ const char *fileName;
310+ /* アプリバイナリの読み込み */
311+ if (env->mainArgc <= 1) {
312+ //アプリ名未指定なので何事もなく終了
313+ exit(EXIT_SUCCESS);
314+ }
315+ fileName = env->mainArgv[1];
316+ //アプリ名先頭に:n:をつけることでレベルnでの実行が可能?
317+ if (fileName[0] == ':' && fileName[2] == ':') {
318+ env->executionLevel = fileName[1] - '0';
319+ if (env->executionLevel < 0 || env->executionLevel > 9){
320+ env->executionLevel = JITC_LV_SLOWEST;
321+ }
322+ fileName += 3;
323+ }
324+
325+ fp = fopen(fileName, "rb");
326+ if (fp == NULL) {
327+ fputs("app-file load error.\n", stderr);
328+ exit(EXIT_FAILURE);
329+ }
330+ env->appSize0 = fread(env->appBin, 1, APPSIZ1 - 4, fp);
331+ env->appSize1 = env->appSize0;
332+ fclose(fp);
333+
334+ if (env->appSize0 >= APPSIZ1 - 4) {
335+ fputs("app-file too large.\n", stderr);
336+ exit(EXIT_FAILURE);
337+ }
338+ if (env->appSize0 < 3) {
339+ fputs("app-file header error.\n", stderr);
340+ exit(EXIT_FAILURE);
341+ }
342+}
--- a/osecpu.h
+++ b/osecpu.h
@@ -1,224 +1,225 @@
1-#ifndef _HDLOAD_OSECPU
2-#define _HDLOAD_OSECPU 1
3-
4-/* Visual Studio で fopen()やsprintf() などの古い関数を使用する時に出る警告を抑止する*/
5-#define _CRT_SECURE_NO_WARNINGS 1
6-
7-//
8-// Including stdc headers
9-//
10-
11-#include <stdio.h>
12-#include <string.h>
13-#include <limits.h>
14-#include <time.h>
15-#include <setjmp.h>
16-#include <stdlib.h>
17-
18-
19-//
20-// Compile options
21-//
22-
23-// Target architecture
24-// 1 : i386
25-#define JITC_ARCNUM 0x0001
26-
27-// Target operating system
28-// 1 : Windows 32bit
29-// 2 : Mac OSX 32bit
30-// 3 : blike for Linux
31-#ifdef _WIN32
32-#define DRV_OSNUM 0x0001
33-#endif
34-#ifdef __APPLE__
35-#define DRV_OSNUM 0x0002
36-#endif
37-#ifdef __linux__
38-#define DRV_OSNUM 0x0003
39-#endif
40-//#define DRV_OSNUM 0x0002
41-
42-// Decoder (syslib.ose) setting
43-// syslib.ose is necessary to work OSECPU
44-#define SYSLIB_OSE "syslib.ose"
45-
46-//
47-// Define constant values
48-//
49-
50-// SIGN1: The 2nd signature of OSECPU Format(05 e1)
51-// It will be changed in OSECPU Rev.2 to "e2" (no adaptation in their binary layers)
52-#define SIGN1 0xe1
53-
54-#define USE_DEBUGGER 1
55-#define USE_TEK5 0 //元々は1
56-
57-/* JITC mode flags */
58-#define JITC_LV_SLOWEST 0 /* デバッグ支援は何でもやる */
59-#define JITC_LV_SLOWER 1 /* エラーモジュールはレポートできるが、行番号は分からない、テストは過剰 */
60-#define JITC_LV_SAFE 2 /* とにかく止まる、場所は不明、テストは必要最小限 */
61-#define JITC_LV_FASTER 4 /* 情報は生成するがチェックをしない */
62-#define JITC_LV_FASTEST 5 /* 情報すら生成しない */
63-#define JITC_PHASE1 0x0001
64-#define JITC_SKIPCHECK 0x0002 /* セキュリティチェックを省略する(高速危険モード) */
65-#define JITC_NOSTARTUP 0x0004
66-#define JITC_MAXLABELS 4096
67-#define PTRCTRLSIZ 4096
68-
69-#define APPSIZ1 1 * 1024 * 1024 /* 1MB for now */
70-#define JITSIZ1 1 * 1024 * 1024 /* 1MB for now */
71-#define SJITSIZ1 1 * 1024 * 1024 /* 1MB for now */
72-#define SYSLIBSIZ1 1 * 1024 * 1024 /* 1MB for now */
73-#define SYSTMP0SIZ 1 * 1024 * 1024 /* 1MB for now */
74-#define SYSTMP1SIZ 2 * 1024 * 1024 /* 1MB for now */
75-
76-#define KEYBUFSIZ 4096
77-
78-#define KEY_ENTER '\n'
79-#define KEY_ESC 27
80-#define KEY_BACKSPACE 8
81-#define KEY_TAB 9
82-#define KEY_PAGEUP 0x1020
83-#define KEY_PAGEDWN 0x1021
84-#define KEY_END 0x1022
85-#define KEY_HOME 0x1023
86-#define KEY_LEFT 0x1024
87-#define KEY_UP 0x1025
88-#define KEY_RIGHT 0x1026
89-#define KEY_DOWN 0x1027
90-#define KEY_INS 0x1028
91-#define KEY_DEL 0x1029
92-
93-//
94-// HOSECPU structures
95-//
96-typedef struct PtrCtrl HOSECPU_PointerControlTag;
97-struct PtrCtrl {
98- int liveSign;
99- int size, typ;
100- unsigned char *p0;
101-};
102-
103-typedef struct Ptr HOSECPU_PointerRegisterEntry;
104-struct Ptr { /* 32バイト(=256bit!) */
105- unsigned char *p;
106- int typ;
107- unsigned char *p0, *p1;
108- int liveSign;
109- HOSECPU_PointerControlTag *pls;
110- int flags, dummy; /* read/writeなど */
111-};
112-
113-typedef struct LabelTable HOSECPU_LabelListTag;
114-struct LabelTable {
115- unsigned char *p, *p1;
116- int opt;
117- /*
118- * default = -1
119- * TYP_CODE = 0
120- * T_UINT8 = 3
121- *
122- * 将来的には UInt8, SInt32, Flt64, UInt8, VPtr が使えるようになる http://osecpu.osask.jp/wiki/?page0053
123- */
124- int typ;
125-};
126-
127-typedef struct Device_Window HOSECPU_Device_Window;
128-struct Device_Window {
129- int *vram;
130- int xsize, ysize;
131-};
132-
133-typedef struct Regs HOSECPU_RuntimeEnvironment;
134-struct Regs {
135- int ireg[64]; /* 32bit整数レジスタ */
136- HOSECPU_PointerRegisterEntry preg[64]; /* ポインタレジスタ */
137- //
138- int debugInfo0, debugInfo1, dmy[2]; /* 2304 */
139- HOSECPU_PointerControlTag *ptrCtrl; /* 2320 */
140- char winClosed, autoSleep;
141- jmp_buf *setjmpEnv;
142-
143- /* Main environment */
144- int mainArgc; // HOSECPU起動引数の個数
145- const char **mainArgv; // HOSECPU起動引数リスト
146- unsigned char *appBin; // 実行するアプリのバイナリ
147- int appSize0;
148- int appSize1;
149- int executionLevel;
150-
151- /* for-junkApi */
152- unsigned char *buf0, *buf1, *junkStack, lastConsoleChar, *junkStack1;
153-
154- HOSECPU_LabelListTag *label;
155- int maxLabels;
156- unsigned char *jitbuf, *jitbuf1;
157- void(*errHndl)(HOSECPU_RuntimeEnvironment *);
158- char dbgr;
159- int mapDi1s[16][16];
160-};
161-
162-//
163-// Grobal values
164-//
165-
166-extern int *keybuf, keybuf_r, keybuf_w, keybuf_c;
167-extern HOSECPU_Device_Window mainWindow;
168-// di1_serial: デバッグ用。プログラム中の随所で加算される変数
169-extern int di1_serial;
170-
171-//
172-// Functions
173-//
174-
175-// @main.c
176-void putKeybuf(int i);
177-int HeavyOSECPUMain(int argc, char **argv);
178-
179-// @comlib.c
180-unsigned char *ComLib_main(const unsigned char *p, unsigned char *q);
181-// @dpndenv.c
182-// OSに依存する関数群を定義する。
183-void *mallocRWE(int bytes); // 実行権付きメモリのmalloc.
184-void drv_openWin(int x, int y, unsigned char *buf, char *winClosed);
185-void drv_flshWin(int sx, int sy, int x0, int y0);
186-void drv_sleep(int msec);
187-
188-// @function.c
189-void dbgrMain(HOSECPU_RuntimeEnvironment *r);
190-const char *searchArg(int argc, const char **argv, const char *tag, int i); // コマンドライン引数処理.
191-void devFunc(HOSECPU_RuntimeEnvironment *r); // junkApiを処理する関数
192-
193-// @jitc.c
194-int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label);
195-int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *src, const unsigned char *src1, const unsigned char *src0, HOSECPU_LabelListTag *label, int maxLabels, int level, int debugInfo1, int flags);
196-unsigned char *jitCompCallFunc(unsigned char *dst, void *func);
197-unsigned char *jitCompInit(unsigned char *dst);
198-void errorHandler(HOSECPU_RuntimeEnvironment *r);
199-
200-// @randmt.c
201-void randStatInit(unsigned int seed);
202-void randStatNext();
203-unsigned int randGetNextUInt32(void);
204-
205-// @screen.c
206-static int iColor1[] = {
207- 0x000000, 0xff0000, 0x00ff00, 0xffff00,
208- 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff
209-};
210-void putOsaskChar(int c, HOSECPU_RuntimeEnvironment *r);
211-void checkString(HOSECPU_RuntimeEnvironment *r, int rxx, int pxx);
212-void checkRect(HOSECPU_RuntimeEnvironment *r, int rxx);
213-int loadColor(HOSECPU_RuntimeEnvironment *r, int rxx);
214-
215-#if (USE_TEK5 != 0)
216-/* tek.cを移植するのは大変だと思ったので、断念 */
217-#error "tek is not supported. edit switch.h and set USE_TEK5=0"
218-// #include "tek.c"
219-// int tek5Decomp(unsigned char *buf, unsigned char *buf1, unsigned char *tmp);
220-#endif
221-
222-
223-#endif
224-
1+#ifndef _HDLOAD_OSECPU
2+#define _HDLOAD_OSECPU 1
3+
4+/* Visual Studio で fopen()やsprintf() などの古い関数を使用する時に出る警告を抑止する*/
5+#define _CRT_SECURE_NO_WARNINGS 1
6+
7+//
8+// Including stdc headers
9+//
10+
11+#include <stdio.h>
12+#include <string.h>
13+#include <limits.h>
14+#include <time.h>
15+#include <setjmp.h>
16+#include <stdlib.h>
17+
18+
19+//
20+// Compile options
21+//
22+
23+// Target architecture
24+// 1 : i386
25+#define JITC_ARCNUM 0x0001
26+
27+// Target operating system
28+// 1 : Windows 32bit
29+// 2 : Mac OSX 32bit
30+// 3 : blike for Linux
31+#ifdef _WIN32
32+#define DRV_OSNUM 0x0001
33+#endif
34+#ifdef __APPLE__
35+#define DRV_OSNUM 0x0002
36+#endif
37+#ifdef __linux__
38+#define DRV_OSNUM 0x0003
39+#endif
40+//#define DRV_OSNUM 0x0002
41+
42+// Decoder (syslib.ose) setting
43+// syslib.ose is necessary to work OSECPU
44+#define SYSLIB_OSE "syslib.ose"
45+
46+//
47+// Define constant values
48+//
49+
50+// SIGN1: The 2nd signature of OSECPU Format(05 e1)
51+// It will be changed in OSECPU Rev.2 to "e2" (no adaptation in their binary layers)
52+#define SIGN1 0xe1
53+
54+#define USE_DEBUGGER 1
55+#define USE_TEK5 0 //元々は1
56+
57+/* JITC mode flags */
58+#define JITC_LV_SLOWEST 0 /* デバッグ支援は何でもやる */
59+#define JITC_LV_SLOWER 1 /* エラーモジュールはレポートできるが、行番号は分からない、テストは過剰 */
60+#define JITC_LV_SAFE 2 /* とにかく止まる、場所は不明、テストは必要最小限 */
61+#define JITC_LV_FASTER 4 /* 情報は生成するがチェックをしない */
62+#define JITC_LV_FASTEST 5 /* 情報すら生成しない */
63+#define JITC_PHASE1 0x0001
64+#define JITC_SKIPCHECK 0x0002 /* セキュリティチェックを省略する(高速危険モード) */
65+#define JITC_NOSTARTUP 0x0004
66+#define JITC_MAXLABELS 4096
67+#define PTRCTRLSIZ 4096
68+
69+#define APPSIZ1 1 * 1024 * 1024 /* 1MB for now */
70+#define JITSIZ1 1 * 1024 * 1024 /* 1MB for now */
71+#define SJITSIZ1 1 * 1024 * 1024 /* 1MB for now */
72+#define SYSLIBSIZ1 1 * 1024 * 1024 /* 1MB for now */
73+#define SYSTMP0SIZ 1 * 1024 * 1024 /* 1MB for now */
74+#define SYSTMP1SIZ 2 * 1024 * 1024 /* 1MB for now */
75+
76+#define KEYBUFSIZ 4096
77+
78+#define KEY_ENTER '\n'
79+#define KEY_ESC 27
80+#define KEY_BACKSPACE 8
81+#define KEY_TAB 9
82+#define KEY_PAGEUP 0x1020
83+#define KEY_PAGEDWN 0x1021
84+#define KEY_END 0x1022
85+#define KEY_HOME 0x1023
86+#define KEY_LEFT 0x1024
87+#define KEY_UP 0x1025
88+#define KEY_RIGHT 0x1026
89+#define KEY_DOWN 0x1027
90+#define KEY_INS 0x1028
91+#define KEY_DEL 0x1029
92+
93+//
94+// HOSECPU structures
95+//
96+typedef struct PtrCtrl HOSECPU_PointerControlTag;
97+struct PtrCtrl {
98+ int liveSign;
99+ int size, typ;
100+ unsigned char *p0;
101+};
102+
103+typedef struct Ptr HOSECPU_PointerRegisterEntry;
104+struct Ptr { /* 32バイト(=256bit!) */
105+ unsigned char *p;
106+ int typ;
107+ unsigned char *p0, *p1;
108+ int liveSign;
109+ HOSECPU_PointerControlTag *pls;
110+ int flags, dummy; /* read/writeなど */
111+};
112+
113+typedef struct LabelTable HOSECPU_LabelListTag;
114+struct LabelTable {
115+ unsigned char *p, *p1;
116+ int opt;
117+ /*
118+ * default = -1
119+ * TYP_CODE = 0
120+ * T_UINT8 = 3
121+ *
122+ * 将来的には UInt8, SInt32, Flt64, UInt8, VPtr が使えるようになる http://osecpu.osask.jp/wiki/?page0053
123+ */
124+ int typ;
125+};
126+
127+typedef struct Device_Window HOSECPU_Device_Window;
128+struct Device_Window {
129+ int *vram;
130+ int xsize, ysize;
131+};
132+
133+typedef struct Regs HOSECPU_RuntimeEnvironment;
134+struct Regs {
135+ int ireg[64]; /* 32bit整数レジスタ */
136+ HOSECPU_PointerRegisterEntry preg[64]; /* ポインタレジスタ */
137+ //
138+ int debugInfo0, debugInfo1, dmy[2]; /* 2304 */
139+ HOSECPU_PointerControlTag *ptrCtrl; /* 2320 */
140+ char winClosed, autoSleep;
141+ jmp_buf setjmpEnv;
142+ int appReturnCode; // アプリ自体の終了コード
143+
144+ /* Main environment */
145+ int mainArgc; // HOSECPU起動引数の個数
146+ const char **mainArgv; // HOSECPU起動引数リスト
147+ unsigned char *appBin; // 実行するアプリのバイナリ
148+ int appSize0;
149+ int appSize1;
150+ int executionLevel;
151+
152+ /* for-junkApi */
153+ unsigned char *buf0, *buf1, *junkStack, lastConsoleChar, *junkStack1;
154+
155+ HOSECPU_LabelListTag *label;
156+ int maxLabels;
157+ unsigned char *jitbuf, *jitbuf1;
158+ void(*errHndl)(HOSECPU_RuntimeEnvironment *);
159+ char dbgr;
160+ int mapDi1s[16][16];
161+};
162+
163+//
164+// Grobal values
165+//
166+
167+extern int *keybuf, keybuf_r, keybuf_w, keybuf_c;
168+extern HOSECPU_Device_Window mainWindow;
169+// di1_serial: デバッグ用。プログラム中の随所で加算される変数
170+extern int di1_serial;
171+
172+//
173+// Functions
174+//
175+
176+// @main.c
177+void putKeybuf(int i);
178+int HeavyOSECPUMain(int argc, char **argv);
179+
180+// @comlib.c
181+unsigned char *ComLib_main(const unsigned char *p, unsigned char *q);
182+// @dpndenv.c
183+// OSに依存する関数群を定義する。
184+void *mallocRWE(int bytes); // 実行権付きメモリのmalloc.
185+void drv_openWin(int x, int y, unsigned char *buf, char *winClosed);
186+void drv_flshWin(int sx, int sy, int x0, int y0);
187+void drv_sleep(int msec);
188+
189+// @function.c
190+void dbgrMain(HOSECPU_RuntimeEnvironment *r);
191+const char *searchArg(int argc, const char **argv, const char *tag, int i); // コマンドライン引数処理.
192+void devFunc(HOSECPU_RuntimeEnvironment *r); // junkApiを処理する関数
193+
194+// @jitc.c
195+int jitc0(unsigned char **qq, unsigned char *q1, const unsigned char *p0, const unsigned char *p1, int level, HOSECPU_LabelListTag *label);
196+int jitCompiler(unsigned char *dst, unsigned char *dst1, const unsigned char *src, const unsigned char *src1, const unsigned char *src0, HOSECPU_LabelListTag *label, int maxLabels, int level, int debugInfo1, int flags);
197+unsigned char *jitCompCallFunc(unsigned char *dst, void *func);
198+unsigned char *jitCompInit(unsigned char *dst);
199+void errorHandler(HOSECPU_RuntimeEnvironment *r);
200+
201+// @randmt.c
202+void randStatInit(unsigned int seed);
203+void randStatNext();
204+unsigned int randGetNextUInt32(void);
205+
206+// @screen.c
207+static int iColor1[] = {
208+ 0x000000, 0xff0000, 0x00ff00, 0xffff00,
209+ 0x0000ff, 0xff00ff, 0x00ffff, 0xffffff
210+};
211+void putOsaskChar(int c, HOSECPU_RuntimeEnvironment *r);
212+void checkString(HOSECPU_RuntimeEnvironment *r, int rxx, int pxx);
213+void checkRect(HOSECPU_RuntimeEnvironment *r, int rxx);
214+int loadColor(HOSECPU_RuntimeEnvironment *r, int rxx);
215+
216+#if (USE_TEK5 != 0)
217+/* tek.cを移植するのは大変だと思ったので、断念 */
218+#error "tek is not supported. edit switch.h and set USE_TEK5=0"
219+// #include "tek.c"
220+// int tek5Decomp(unsigned char *buf, unsigned char *buf1, unsigned char *tmp);
221+#endif
222+
223+
224+#endif
225+
Show on old repository browser