OSDN Git Service

reader の関数ポインタの引数の型を変更
[unagi/old-svn-converted.git] / client / trunk / reader_kazzo.c
1 #include <assert.h>
2 #include <usb.h>
3 #include "reader_master.h"
4 #include "usb_device.h"
5 #include "../kazzo/request.h"
6 #include "../kazzo/usbconfig.h"
7 #include "reader_kazzo.h"
8
9 static usb_dev_handle *device_open(void)
10 {
11         usb_dev_handle *handle = NULL;
12         const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID};
13         const unsigned char rawPid[2] = {USB_CFG_DEVICE_ID};
14         char vendor[] = {USB_CFG_VENDOR_NAME, 0};
15         char product[] = {USB_CFG_DEVICE_NAME, 0};
16         int vid, pid;
17
18         /* compute VID/PID from usbconfig.h so that there is a central source of information */
19         vid = (rawVid[1] << 8) | rawVid[0];
20         pid = (rawPid[1] << 8) | rawPid[0];
21
22         if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) == 0){
23                 return handle;
24         }
25         fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
26         return NULL;
27 }
28 static usb_dev_handle *handle = NULL;
29 static int kazzo_open_close(enum reader_control oc)
30 {
31         switch(oc){
32         case READER_OPEN:
33                 handle = device_open();
34                 return handle == NULL ? NG : OK;
35         case READER_CLOSE:
36                 usb_close(handle);
37                 handle = NULL;
38                 return OK;
39         }
40         return NG;
41 }
42 static void kazzo_init(void)
43 {
44         //no operation
45 }
46 enum{
47         TIMEOUT = 4000
48 };
49 //-------- read sequence --------
50 static void device_read(usb_dev_handle *handle, enum request r, long address, long length, uint8_t *data)
51 {
52         int cnt = usb_control_msg(
53                 handle, 
54                 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
55                 r, address, 
56                 0, data, length, TIMEOUT
57         );
58         assert(cnt == length);
59 }
60 static void read_main(const enum request r, long address, long length, uint8_t *data)
61 {
62         while(length >= READ_PACKET_SIZE){
63                 device_read(handle, r, address, READ_PACKET_SIZE, data);
64                 data += READ_PACKET_SIZE;
65                 address += READ_PACKET_SIZE;
66                 length -= READ_PACKET_SIZE;
67         }
68         if(length != 0){
69                 device_read(handle, r, address, length, data);
70         }
71 }
72 static void kazzo_cpu_read(long address, long length, uint8_t *data)
73 {
74         read_main(REQUEST_CPU_READ, address, length, data);
75 }
76 static void kazzo_ppu_read(long address, long length, uint8_t *data)
77 {
78         read_main(REQUEST_PPU_READ, address, length, data);
79 }
80 //-------- write sequence --------
81 static void device_write(usb_dev_handle *handle, enum request w, long address, long length, const uint8_t *data)
82 {
83         //const ¤ò³°¤·¤Æ̵ÍýÌðÍýÅϤ¹¤·¤«¤Ê¤¤
84         int cnt = usb_control_msg(
85                 handle, 
86                 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
87                 w, address, 
88                 0, (uint8_t *) data, length, TIMEOUT
89         );
90         assert(cnt == length);
91 }
92
93 static void kazzo_cpu_write_6502(long address, long length, const uint8_t *data)
94 {
95         device_write(handle, REQUEST_CPU_WRITE_6502, address, length, data);
96 }
97 static void kazzo_cpu_write_flash(long address, long data)
98 {
99         uint8_t d = (uint8_t) (data & 0xff);
100         device_write(handle, REQUEST_CPU_WRITE_FLASH, address, 1, &d);
101 }
102 static void kazzo_ppu_write(long address, long length, const uint8_t *data)
103 {
104         device_write(handle, REQUEST_PPU_WRITE, address, length, data);
105 }
106
107 static inline void pack_short_le(long l, uint8_t *t)
108 {
109         t[0] = l & 0xff;
110         t[1] = (l >> 8) & 0xff;
111 }
112 static flash_config(enum request r, long c2aaa, long c5555, long unit)
113 {
114         const int size = 2 * 3;
115         uint8_t t[size];
116         assert(unit >= 1 && unit < 0x400);
117         pack_short_le(c2aaa, t);
118         pack_short_le(c5555, t + 2);
119         pack_short_le(unit, t + 4);
120         device_write(handle, r, 0, size, t);
121 }
122 static void kazzo_cpu_flash_config(long c2aaa, long c5555, long unit)
123 {
124         flash_config(REQUEST_CPU_FLASH_CONFIG_SET, c2aaa, c5555, unit);
125 }
126 static void kazzo_ppu_flash_config(long c2aaa, long c5555, long unit)
127 {
128         flash_config(REQUEST_PPU_FLASH_CONFIG_SET, c2aaa, c5555, unit);
129 }
130
131 static inline void flash_execute(enum request p, enum request s, long address, uint8_t *data, int size)
132 {
133         uint8_t status;
134         device_write(handle, p, address, size, data);
135         do{
136                 wait(1);
137                 device_read(handle, s, 0, 1, &status);
138         }while(status != 0);
139 }
140 static void kazzo_cpu_flash_erase(long address)
141 {
142         flash_execute(REQUEST_CPU_FLASH_ERASE, REQUEST_CPU_FLASH_STATUS, address, NULL, 0);
143 }
144 static void kazzo_ppu_flash_erase(long address)
145 {
146         flash_execute(REQUEST_PPU_FLASH_ERASE, REQUEST_PPU_FLASH_STATUS, address, NULL, 0);
147 }
148
149 static void flash_program(enum request p, enum request s, long address, long length, uint8_t *data)
150 {
151         while(length >= READ_PACKET_SIZE){
152                 flash_execute(p, s, address, data, READ_PACKET_SIZE);
153                 address += READ_PACKET_SIZE;
154                 data += READ_PACKET_SIZE;
155                 length -= READ_PACKET_SIZE;
156         }
157 }
158 static void kazzo_cpu_flash_program(long address, long length, uint8_t *data)
159 {
160         enum request p = REQUEST_CPU_FLASH_PROGRAM;
161         enum request s = REQUEST_CPU_FLASH_STATUS;
162         flash_program(p, s, address, length, data);
163 }
164 static void kazzo_ppu_flash_program(long address, long length, uint8_t *data)
165 {
166         flash_program(REQUEST_PPU_FLASH_PROGRAM, REQUEST_PPU_FLASH_STATUS, address, length, data);
167 }
168
169 const struct reader_driver DRIVER_KAZZO = {
170         .name = "kazzo",
171         .open_or_close = kazzo_open_close,
172         .init = kazzo_init,
173         .cpu_read = kazzo_cpu_read, .ppu_read = kazzo_ppu_read,
174         .cpu_write_6502 = kazzo_cpu_write_6502,
175         .flash_support = OK,
176         .ppu_write = kazzo_ppu_write,
177         .cpu_flash_config = kazzo_cpu_flash_config,
178         .cpu_flash_erase = kazzo_cpu_flash_erase,
179         .cpu_flash_program = kazzo_cpu_flash_program,
180         .ppu_flash_config = kazzo_ppu_flash_config,
181         .ppu_flash_erase = kazzo_ppu_flash_erase,
182         .ppu_flash_program = kazzo_ppu_flash_program
183 };