1 /*
2 * vic20via1.c - VIA1 emulation in the VIC20.
3 *
4 * Written by
5 * Andre Fachat <fachat@physik.tu-chemnitz.de>
6 * Ettore Perazzoli <ettore@comm2000.it>
7 * Andreas Boose <viceteam@t-online.de>
8 *
9 * This file is part of VICE, the Versatile Commodore Emulator.
10 * See README for copyright notice.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 * 02111-1307 USA.
26 *
27 */
28
29 #include "vice.h"
30
31 #include <stdio.h>
32
33 #include "datasette.h"
34 #include "interrupt.h"
35 #include "joyport.h"
36 #include "keyboard.h"
37 #include "lib.h"
38 #include "maincpu.h"
39 #include "tapeport.h"
40 #include "types.h"
41 #include "via.h"
42 #include "vic20.h"
43 #include "vic20iec.h"
44 #include "vic20via.h"
45
46
via1_store(uint16_t addr,uint8_t data)47 void via1_store(uint16_t addr, uint8_t data)
48 {
49 viacore_store(machine_context.via1, addr, data);
50 }
51
via1_read(uint16_t addr)52 uint8_t via1_read(uint16_t addr)
53 {
54 return viacore_read(machine_context.via1, addr);
55 }
56
via1_peek(uint16_t addr)57 uint8_t via1_peek(uint16_t addr)
58 {
59 return viacore_peek(machine_context.via1, addr);
60 }
61
set_ca2(via_context_t * via_context,int state)62 static void set_ca2(via_context_t *via_context, int state)
63 {
64 }
65
set_cb2(via_context_t * via_context,int state)66 static void set_cb2(via_context_t *via_context, int state)
67 {
68 }
69
set_int(via_context_t * via_context,unsigned int int_num,int value,CLOCK rclk)70 static void set_int(via_context_t *via_context, unsigned int int_num,
71 int value, CLOCK rclk)
72 {
73 interrupt_set_irq(maincpu_int_status, int_num, value, rclk);
74 }
75
restore_int(via_context_t * via_context,unsigned int int_num,int value)76 static void restore_int(via_context_t *via_context, unsigned int int_num, int value)
77 {
78 interrupt_restore_irq(maincpu_int_status, int_num, value);
79 }
80
undump_acr(via_context_t * via_context,uint8_t byte)81 static void undump_acr(via_context_t *via_context, uint8_t byte)
82 {
83 }
84
store_acr(via_context_t * via_context,uint8_t byte)85 static void store_acr(via_context_t *via_context, uint8_t byte)
86 {
87 }
88
store_sr(via_context_t * via_context,uint8_t byte)89 static void store_sr(via_context_t *via_context, uint8_t byte)
90 {
91 }
92
store_t2l(via_context_t * via_context,uint8_t byte)93 static void store_t2l(via_context_t *via_context, uint8_t byte)
94 {
95 }
96
undump_pra(via_context_t * via_context,uint8_t byte)97 static void undump_pra(via_context_t *via_context, uint8_t byte)
98 {
99 }
100
store_pra(via_context_t * via_context,uint8_t byte,uint8_t myoldpa,uint16_t addr)101 static void store_pra(via_context_t *via_context, uint8_t byte, uint8_t myoldpa,
102 uint16_t addr)
103 {
104 }
105
undump_prb(via_context_t * via_context,uint8_t byte)106 static void undump_prb(via_context_t *via_context, uint8_t byte)
107 {
108 }
109
store_prb(via_context_t * via_context,uint8_t byte,uint8_t myoldpb,uint16_t addr)110 static void store_prb(via_context_t *via_context, uint8_t byte, uint8_t myoldpb,
111 uint16_t addr)
112 {
113 uint8_t joy_bit = (byte & 0x80) >> 4;
114
115 if ((byte ^ myoldpb) & 8) {
116 tapeport_toggle_write_bit((~(via_context->via[VIA_DDRB]) | byte) & 0x8);
117 }
118 store_joyport_dig(JOYPORT_1, joy_bit, 8);
119 }
120
undump_pcr(via_context_t * via_context,uint8_t byte)121 static void undump_pcr(via_context_t *via_context, uint8_t byte)
122 {
123 }
124
reset(via_context_t * via_context)125 static void reset(via_context_t *via_context)
126 {
127 /*iec_pcr_write(0x22);*/
128 }
129
store_pcr(via_context_t * via_context,uint8_t byte,uint16_t addr)130 static uint8_t store_pcr(via_context_t *via_context, uint8_t byte, uint16_t addr)
131 {
132 /* FIXME: this should use via_set_ca2() and via_set_cb2() */
133 if (byte != via_context->via[VIA_PCR]) {
134 register uint8_t tmp = byte;
135 /* first set bit 1 and 5 to the real output values */
136 if ((tmp & 0x0c) != 0x0c) {
137 tmp |= 0x02;
138 }
139 if ((tmp & 0xc0) != 0xc0) {
140 tmp |= 0x20;
141 }
142 iec_pcr_write(tmp);
143 }
144 return byte;
145 }
146
read_pra(via_context_t * via_context,uint16_t addr)147 static uint8_t read_pra(via_context_t *via_context, uint16_t addr)
148 {
149 uint8_t byte;
150 /* FIXME: not 100% sure about this... */
151 uint8_t val = ~(via_context->via[VIA_DDRA]);
152 uint8_t msk = via_context->oldpb;
153 uint8_t m;
154 int i;
155
156 for (m = 0x1, i = 0; i < 8; m <<= 1, i++) {
157 if (!(msk & m)) {
158 val &= ~rev_keyarr[i];
159 }
160 }
161
162 byte = val | (via_context->via[VIA_PRA] & via_context->via[VIA_DDRA]);
163 return byte;
164 }
165
read_prb(via_context_t * via_context)166 static uint8_t read_prb(via_context_t *via_context)
167 {
168 uint8_t byte;
169 /* FIXME: not 100% sure about this... */
170 uint8_t val = ~(via_context->via[VIA_DDRB]);
171 uint8_t msk = via_context->oldpa;
172 uint8_t joy = ~read_joyport_dig(JOYPORT_1);
173 int m, i;
174
175 for (m = 0x1, i = 0; i < 8; m <<= 1, i++) {
176 if (!(msk & m)) {
177 val &= ~keyarr[i];
178 }
179 }
180
181 /* Bit 7 is mapped to the right direction of the joystick (bit
182 3 in `joystick_value[]'). */
183 if (joy & 0x8) {
184 val &= 0x7f;
185 }
186
187 byte = val | (via_context->via[VIA_PRB] & via_context->via[VIA_DDRB]);
188 return byte;
189 }
190
via1_init(via_context_t * via_context)191 void via1_init(via_context_t *via_context)
192 {
193 viacore_init(machine_context.via1, maincpu_alarm_context,
194 maincpu_int_status, maincpu_clk_guard);
195 }
196
vic20via1_setup_context(machine_context_t * machinecontext)197 void vic20via1_setup_context(machine_context_t *machinecontext)
198 {
199 via_context_t *via;
200
201 machinecontext->via1 = lib_malloc(sizeof(via_context_t));
202 via = machinecontext->via1;
203
204 via->prv = NULL;
205 via->context = NULL;
206
207 via->rmw_flag = &maincpu_rmw_flag;
208 via->clk_ptr = &maincpu_clk;
209
210 via->myname = lib_msprintf("Via1");
211 via->my_module_name = lib_msprintf("VIA1");
212
213 viacore_setup_context(via);
214
215 via->write_offset = 0;
216
217 via->irq_line = IK_IRQ;
218
219 via->undump_pra = undump_pra;
220 via->undump_prb = undump_prb;
221 via->undump_pcr = undump_pcr;
222 via->undump_acr = undump_acr;
223 via->store_pra = store_pra;
224 via->store_prb = store_prb;
225 via->store_pcr = store_pcr;
226 via->store_acr = store_acr;
227 via->store_sr = store_sr;
228 via->store_t2l = store_t2l;
229 via->read_pra = read_pra;
230 via->read_prb = read_prb;
231 via->set_int = set_int;
232 via->restore_int = restore_int;
233 via->set_ca2 = set_ca2;
234 via->set_cb2 = set_cb2;
235 via->reset = reset;
236 }
237