OSDN Git Service

unicode support
[unagi/old-svn-converted.git] / client / trunk / anago / script_common.c
1 #include <assert.h>
2 #include <string.h>
3 #include <squirrel.h>
4 #include <sqstdio.h>
5 #include <sqstdaux.h>
6 #include "type.h"
7 #include "squirrel_wrap.h"
8 #include "reader_master.h"
9 #include "widget.h"
10 #include "script_common.h"
11
12 SQInteger script_nop(HSQUIRRELVM v)
13 {
14         return 0;
15 }
16
17 SQInteger range_check(HSQUIRRELVM v, const wgChar *name, long target, const struct range *range)
18 {
19         if((target < range->start) || (target > range->end)){
20                 SQPRINTFUNCTION f = sq_getprintfunc(v);
21                 f(v, wgT("%s range must be 0x%06x to 0x%06x"), name, (int) range->start, (int) range->end);
22                 return sq_throwerror(v, wgT("script logical error"));
23         }
24         return 0;
25 }
26
27 SQInteger cpu_write_check(HSQUIRRELVM v)
28 {
29         static const struct range range_address = {0x4000, 0x10000};
30         static const struct range range_data = {0x0, 0xff};
31         long address, data;
32         SQRESULT r = qr_argument_get(v, 2, &address, &data);
33         if(SQ_FAILED(r)){
34                 return r;
35         }
36         r = range_check(v, wgT("address"), address, &range_address);
37         if(SQ_FAILED(r)){
38                 return r;
39         }
40         return range_check(v, wgT("data"), data, &range_data);
41 }
42
43 SQInteger script_require(HSQUIRRELVM v)
44 {
45         if(sq_gettop(v) != 2){
46                 return sq_throwerror(v, wgT("argument number error"));
47         }
48         if(sq_gettype(v, 2) != OT_STRING){
49                 return sq_throwerror(v, wgT("argument type error"));
50         }
51         const SQChar *file;
52         if(SQ_FAILED(sq_getstring(v, 2, &file))){
53                 return sq_throwerror(v, wgT("require error"));
54         }
55         if(SQ_FAILED(sqstd_dofile(v, file, SQFalse, SQTrue))){
56                 return sq_throwerror(v, wgT("require error"));
57         }
58         return 0;
59 }
60
61 static bool connection_check_main(const struct reader_handle *h, const struct textcontrol *text, const struct reader_memory_access *m, long address)
62 {
63         const int size = 0x10;
64         uint8_t test1[size], test_m[size];
65         int i;
66         
67         m->memory_read(h, &GAUGE_DUMMY, address, size, test1);
68         for(i = 0; i < 3; i++){
69                 m->memory_read(h, &GAUGE_DUMMY, address, size, test_m);
70                 if(memcmp(test1, test_m, size) != 0){
71                         text->append(text->object, wgT("maybe cartridge connection error\n"));
72                         return false;
73                 }
74         }
75         return true;
76 }
77 bool connection_check(const struct reader_handle *h, const struct textcontrol *text, const struct reader_memory_access *cpu, const struct reader_memory_access *ppu)
78 {
79         if(connection_check_main(h, text, cpu, 0xf000) == false){
80                 return false;
81         }
82         if(connection_check_main(h, text, ppu, 0x0000) == false){
83                 return false;
84         }
85         return true;
86 }