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"
9 static usb_dev_handle *device_open(void)
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};
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];
22 if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) == 0){
25 fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
28 static usb_dev_handle *handle = NULL;
29 static int kazzo_open_close(enum reader_control oc)
33 handle = device_open();
34 return handle == NULL ? NG : OK;
42 static void kazzo_init(void)
49 //-------- read sequence --------
50 static void device_read(usb_dev_handle *handle, enum request r, long address, long length, uint8_t *data)
52 int cnt = usb_control_msg(
54 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
56 0, data, length, TIMEOUT
58 assert(cnt == length);
60 static void read_main(const enum request r, long address, long length, uint8_t *data)
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;
69 device_read(handle, r, address, length, data);
72 static void kazzo_cpu_read(long address, long length, uint8_t *data)
74 read_main(REQUEST_CPU_READ, address, length, data);
76 static void kazzo_ppu_read(long address, long length, uint8_t *data)
78 read_main(REQUEST_PPU_READ, address, length, data);
80 //-------- write sequence --------
81 static void device_write(usb_dev_handle *handle, enum request w, long address, long length, const uint8_t *data)
83 //const ¤ò³°¤·¤Æ̵ÍýÌðÍýÅϤ¹¤·¤«¤Ê¤¤
84 int cnt = usb_control_msg(
86 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
88 0, (uint8_t *) data, length, TIMEOUT
90 assert(cnt == length);
93 static void kazzo_cpu_write_6502(long address, long length, const uint8_t *data)
95 device_write(handle, REQUEST_CPU_WRITE_6502, address, length, data);
97 static void kazzo_cpu_write_flash(long address, long data)
99 uint8_t d = (uint8_t) (data & 0xff);
100 device_write(handle, REQUEST_CPU_WRITE_FLASH, address, 1, &d);
102 static void kazzo_ppu_write(long address, long length, const uint8_t *data)
104 device_write(handle, REQUEST_PPU_WRITE, address, length, data);
107 static inline void pack_short_le(long l, uint8_t *t)
110 t[1] = (l >> 8) & 0xff;
112 static flash_config(enum request r, long c2aaa, long c5555, long unit)
114 const int size = 2 * 3;
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);
122 static void kazzo_cpu_flash_config(long c2aaa, long c5555, long unit)
124 flash_config(REQUEST_CPU_FLASH_CONFIG_SET, c2aaa, c5555, unit);
126 static void kazzo_ppu_flash_config(long c2aaa, long c5555, long unit)
128 flash_config(REQUEST_PPU_FLASH_CONFIG_SET, c2aaa, c5555, unit);
131 static inline void flash_execute(enum request p, enum request s, long address, uint8_t *data, int size)
134 device_write(handle, p, address, size, data);
137 device_read(handle, s, 0, 1, &status);
140 static void kazzo_cpu_flash_erase(long address)
142 flash_execute(REQUEST_CPU_FLASH_ERASE, REQUEST_CPU_FLASH_STATUS, address, NULL, 0);
144 static void kazzo_ppu_flash_erase(long address)
146 flash_execute(REQUEST_PPU_FLASH_ERASE, REQUEST_PPU_FLASH_STATUS, address, NULL, 0);
149 static void flash_program(enum request p, enum request s, long address, long length, uint8_t *data)
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;
158 static void kazzo_cpu_flash_program(long address, long length, uint8_t *data)
160 enum request p = REQUEST_CPU_FLASH_PROGRAM;
161 enum request s = REQUEST_CPU_FLASH_STATUS;
162 flash_program(p, s, address, length, data);
164 static void kazzo_ppu_flash_program(long address, long length, uint8_t *data)
166 flash_program(REQUEST_PPU_FLASH_PROGRAM, REQUEST_PPU_FLASH_STATUS, address, length, data);
169 const struct reader_driver DRIVER_KAZZO = {
171 .open_or_close = kazzo_open_close,
173 .cpu_read = kazzo_cpu_read, .ppu_read = kazzo_ppu_read,
174 .cpu_write_6502 = kazzo_cpu_write_6502,
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