]> git.lizzy.rs Git - plan9front.git/blob - sys/src/games/nes/state.c
bring games/swar from 1ed sources.
[plan9front.git] / sys / src / games / nes / state.c
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include <draw.h>
5 #include "dat.h"
6 #include "fns.h"
7
8 static int fd;
9
10 void
11 put8(u8int i)
12 {
13         write(fd, &i, 1);
14 }
15
16 void
17 put16(u16int i)
18 {
19         put8(i);
20         put8(i >> 8);
21 }
22
23 void
24 put32(u32int i)
25 {
26         put8(i);
27         put8(i >> 8);
28         put8(i >> 16);
29         put8(i >> 24);
30 }
31
32 int
33 get8(void)
34 {
35         u8int c;
36         
37         read(fd, &c, 1);
38         return c;
39 }
40
41 int
42 get16(void)
43 {
44         int i;
45         
46         i = get8();
47         i |= get8() << 8;
48         return i;
49 }
50
51 int
52 get32(void)
53 {
54         int i;
55         
56         i = get8();
57         i |= get8() << 8;
58         i |= get8() << 16;
59         i |= get8() << 24;
60         return i;
61 }
62
63 void
64 loadstate(char *file)
65 {
66         fd = open(file, OREAD);
67         if(fd < 0){
68                 message("open: %r");
69                 return;
70         }
71         read(fd, mem, sizeof(mem));
72         read(fd, ppuram, sizeof(ppuram));
73         read(fd, oam, sizeof(oam));
74         if(chrram)
75                 read(fd, chr, nchr * CHRSZ);
76         rA = get8();
77         rX = get8();
78         rY = get8();
79         rS = get8();
80         rP = get8();
81         nmi = get8();
82         pc = get16();
83         pput = get16();
84         ppuv = get16();
85         ppusx = get8();
86         ppux = get16();
87         ppuy = get16();
88         mirr = get8();
89         odd = get8();
90         vramlatch = get8();
91         keylatch = get8();
92         keylatch2 = get8();
93         vrambuf = get8();
94         clock = get32();
95         ppuclock = get32();
96         apuclock = get32();
97         apuseq = get8();
98         dmcaddr = get16();
99         dmccnt = get16();
100         read(fd, apuctr, sizeof(apuctr));
101         mapper[map](RSTR, 0);
102         close(fd);
103 }
104
105 void
106 savestate(char *file)
107 {
108         fd = create(file, ORDWR, 0666);
109         if(fd < 0){
110                 message("create: %r");
111                 return;
112         }
113         write(fd, mem, sizeof(mem));
114         write(fd, ppuram, sizeof(ppuram));
115         write(fd, oam, sizeof(oam));
116         if(chrram)
117                 write(fd, chr, nchr * CHRSZ);
118         put8(rA);
119         put8(rX);
120         put8(rY);
121         put8(rS);
122         put8(rP);
123         put8(nmi);
124         put16(pc);
125         put16(pput);
126         put16(ppuv);
127         put8(ppusx);
128         put16(ppux);
129         put16(ppuy);
130         put8(mirr);
131         put8(odd);
132         put8(vramlatch);
133         put8(keylatch);
134         put8(keylatch2);
135         put8(vrambuf);
136         put32(clock);
137         put32(ppuclock);
138         put32(apuclock);
139         put8(apuseq);
140         put16(dmcaddr);
141         put16(dmccnt);
142         write(fd, apuctr, sizeof(apuctr));
143         mapper[map](SAVE, 0);
144         close(fd);
145 }