14 #define RDY(n) ((n) == 0 ? RDY1 : RDY2)
16 typedef enum { WAIT
, READ1
, READ2
, READ3
} state_t
;
19 uint8_t *flash_contents
;
22 uint8_t address_cycle
;
25 static tc58128_dev tc58128_devs
[2];
27 #define FLASH_SIZE (16*1024*1024)
29 static void init_dev(tc58128_dev
* dev
, const char *filename
)
34 dev
->flash_contents
= qemu_mallocz(FLASH_SIZE
);
35 memset(dev
->flash_contents
, 0xff, FLASH_SIZE
);
36 if (!dev
->flash_contents
) {
37 fprintf(stderr
, "could not alloc memory for flash\n");
41 /* Load flash image skipping the first block */
42 ret
= load_image(filename
, dev
->flash_contents
+ 528 * 32);
44 fprintf(stderr
, "ret=%d\n", ret
);
45 fprintf(stderr
, "qemu: could not load flash image %s\n",
49 /* Build first block with number of blocks */
50 blocks
= (ret
+ 528 * 32 - 1) / (528 * 32);
51 dev
->flash_contents
[0] = blocks
& 0xff;
52 dev
->flash_contents
[1] = (blocks
>> 8) & 0xff;
53 dev
->flash_contents
[2] = (blocks
>> 16) & 0xff;
54 dev
->flash_contents
[3] = (blocks
>> 24) & 0xff;
55 fprintf(stderr
, "loaded %d bytes for %s into flash\n", ret
,
61 static void handle_command(tc58128_dev
* dev
, uint8_t command
)
65 fprintf(stderr
, "reset flash device\n");
69 fprintf(stderr
, "read mode 1\n");
71 dev
->address_cycle
= 0;
74 fprintf(stderr
, "read mode 2\n");
76 dev
->address_cycle
= 0;
79 fprintf(stderr
, "read mode 3\n");
81 dev
->address_cycle
= 0;
84 fprintf(stderr
, "unknown flash command 0x%02x\n", command
);
89 static void handle_address(tc58128_dev
* dev
, uint8_t data
)
95 switch (dev
->address_cycle
) {
98 if (dev
->state
== READ2
)
99 dev
->address
|= 0x100;
100 else if (dev
->state
== READ3
)
101 dev
->address
|= 0x200;
104 dev
->address
+= data
* 528 * 0x100;
107 dev
->address
+= data
* 528;
108 fprintf(stderr
, "address pointer in flash: 0x%08x\n",
115 dev
->address_cycle
++;
122 static uint8_t handle_read(tc58128_dev
* dev
)
125 if (dev
->address
% 0x100000 == 0)
126 fprintf(stderr
, "reading flash at address 0x%08x\n", dev
->address
);
128 return dev
->flash_contents
[dev
->address
++];
131 /* We never mark the device as busy, so interrupts cannot be triggered
134 static int tc58128_cb(uint16_t porta
, uint16_t portb
,
135 uint16_t * periph_pdtra
, uint16_t * periph_portadir
,
136 uint16_t * periph_pdtrb
, uint16_t * periph_portbdir
)
140 if ((porta
& CE1
) == 0)
142 else if ((porta
& CE2
) == 0)
145 return 0; /* No device selected */
147 if ((porta
& RE
) && (porta
& WE
)) {
148 /* Nothing to do, assert ready and return to input state */
149 *periph_portadir
&= 0xff00;
150 *periph_portadir
|= RDY(dev
);
151 *periph_pdtra
|= RDY(dev
);
157 assert((porta
& WE
) == 0);
158 handle_command(&tc58128_devs
[dev
], porta
& 0x00ff);
159 } else if (porta
& ALE
) {
160 assert((porta
& WE
) == 0);
161 handle_address(&tc58128_devs
[dev
], porta
& 0x00ff);
162 } else if ((porta
& RE
) == 0) {
163 *periph_portadir
|= 0x00ff;
164 *periph_pdtra
&= 0xff00;
165 *periph_pdtra
|= handle_read(&tc58128_devs
[dev
]);
172 static sh7750_io_device tc58128
= {
173 RE
| WE
, /* Port A triggers */
174 0, /* Port B triggers */
175 tc58128_cb
/* Callback */
178 int tc58128_init(struct SH7750State
*s
, const char *zone1
, const char *zone2
)
180 init_dev(&tc58128_devs
[0], zone1
);
181 init_dev(&tc58128_devs
[1], zone2
);
182 return sh7750_register_io_device(s
, &tc58128
);