|
1 |
| -use ::libc; |
2 | 1 | use ::c2rust_bitfields;
|
3 |
| -pub type __int8_t = i8; |
4 |
| -pub type __uint8_t = u8; |
5 |
| -pub type __uint16_t = u16; |
6 |
| -pub type __int32_t = i32; |
7 |
| -pub type __uint32_t = u32; |
8 |
| -pub type int8_t = __int8_t; |
9 |
| -pub type int32_t = __int32_t; |
10 |
| -pub type uint8_t = __uint8_t; |
11 |
| -pub type uint16_t = __uint16_t; |
12 |
| -pub type uint32_t = __uint32_t; |
13 |
| -pub type C2RustUnnamed = u32; |
14 |
| -pub const Z80_ASSERT: C2RustUnnamed = 2; |
15 |
| -pub const Z80_PULSE: C2RustUnnamed = 1; |
16 |
| -#[derive(Copy, Clone, BitfieldStruct)] |
| 2 | +pub type int8_t = i8; |
| 3 | +pub type int32_t = i32; |
| 4 | +pub type uint8_t = u8; |
| 5 | +pub type uint16_t = u16; |
| 6 | +pub type uint32_t = u32; |
| 7 | +pub const Z80_ASSERT: u32 = 2; |
| 8 | +pub const Z80_PULSE: u32 = 1; |
| 9 | + |
| 10 | +pub trait z80Controller { |
| 11 | + fn read_byte(&self, addr: u16) -> u8; |
| 12 | + fn write_byte(&mut self, addr: u16, value: u8); |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(BitfieldStruct)] |
17 | 16 | #[repr(C)]
|
18 | 17 | pub struct z80 {
|
19 |
| - pub read_byte: Option::< |
20 |
| - unsafe extern "C" fn(*mut libc::c_void, uint16_t) -> uint8_t, |
21 |
| - >, |
22 |
| - pub write_byte: Option::< |
23 |
| - unsafe extern "C" fn(*mut libc::c_void, uint16_t, uint8_t) -> (), |
24 |
| - >, |
| 18 | + pub memory: Box<dyn z80Controller>, |
25 | 19 | pub port_in: Option::<unsafe extern "C" fn(*mut z80, uint16_t) -> uint8_t>,
|
26 | 20 | pub port_out: Option::<unsafe extern "C" fn(*mut z80, uint16_t, uint8_t) -> ()>,
|
27 |
| - pub userdata: *mut libc::c_void, |
28 | 21 | pub pc: uint16_t,
|
29 | 22 | pub sp: uint16_t,
|
30 | 23 | pub ix: uint16_t,
|
@@ -183,34 +176,24 @@ unsafe extern "C" fn flag_set(z: *mut z80, mut bit: z80_flagbit, mut val: bool)
|
183 | 176 | | (val as i32) << bit as u32) as uint8_t;
|
184 | 177 | }
|
185 | 178 | #[inline]
|
186 |
| -unsafe extern "C" fn rb(z: *mut z80, mut addr: uint16_t) -> uint8_t { |
187 |
| - return ((*z).read_byte).expect("non-null function pointer")((*z).userdata, addr); |
| 179 | +unsafe fn rb(z: *mut z80, mut addr: uint16_t) -> uint8_t { |
| 180 | + return (*z).memory.read_byte(addr); |
188 | 181 | }
|
189 | 182 | #[inline]
|
190 |
| -unsafe extern "C" fn wb(z: *mut z80, mut addr: uint16_t, mut val: uint8_t) { |
191 |
| - ((*z).write_byte).expect("non-null function pointer")((*z).userdata, addr, val); |
| 183 | +unsafe fn wb(z: *mut z80, mut addr: uint16_t, mut val: uint8_t) { |
| 184 | + (*z).memory.write_byte(addr, val); |
192 | 185 | }
|
193 | 186 | #[inline]
|
194 |
| -unsafe extern "C" fn rw(z: *mut z80, mut addr: uint16_t) -> uint16_t { |
195 |
| - return ((((*z).read_byte) |
196 |
| - .expect( |
197 |
| - "non-null function pointer", |
198 |
| - )((*z).userdata, (addr as i32 + 1 as i32) as uint16_t) |
199 |
| - as i32) << 8 as i32 |
200 |
| - | ((*z).read_byte).expect("non-null function pointer")((*z).userdata, addr) |
201 |
| - as i32) as uint16_t; |
| 187 | +unsafe fn rw(z: *mut z80, mut addr: uint16_t) -> uint16_t { |
| 188 | + return ( |
| 189 | + ((*z).memory.read_byte((addr as i32 + 1 as i32) as uint16_t) as i32) << 8 as i32 |
| 190 | + | (*z).memory.read_byte(addr) as i32 |
| 191 | + ) as uint16_t; |
202 | 192 | }
|
203 | 193 | #[inline]
|
204 |
| -unsafe extern "C" fn ww(z: *mut z80, mut addr: uint16_t, mut val: uint16_t) { |
205 |
| - ((*z).write_byte) |
206 |
| - .expect( |
207 |
| - "non-null function pointer", |
208 |
| - )((*z).userdata, addr, (val as i32 & 0xff as i32) as uint8_t); |
209 |
| - ((*z).write_byte) |
210 |
| - .expect( |
211 |
| - "non-null function pointer", |
212 |
| - )( |
213 |
| - (*z).userdata, |
| 194 | +unsafe fn ww(z: *mut z80, mut addr: uint16_t, mut val: uint16_t) { |
| 195 | + (*z).memory.write_byte(addr, (val as i32 & 0xff as i32) as uint8_t); |
| 196 | + (*z).memory.write_byte( |
214 | 197 | (addr as i32 + 1 as i32) as uint16_t,
|
215 | 198 | (val as i32 >> 8 as i32) as uint8_t,
|
216 | 199 | );
|
@@ -984,36 +967,35 @@ unsafe extern "C" fn process_interrupts(z: *mut z80) -> u32 {
|
984 | 967 | }
|
985 | 968 | return cyc;
|
986 | 969 | }
|
987 |
| -#[no_mangle] |
988 |
| -pub unsafe extern "C" fn z80_init(z: *mut z80) { |
989 |
| - (*z).read_byte = None; |
990 |
| - (*z).write_byte = None; |
991 |
| - (*z).port_in = None; |
992 |
| - (*z).port_out = None; |
993 |
| - (*z).userdata = 0 as *mut libc::c_void; |
994 |
| - (*z).pc = 0 as i32 as uint16_t; |
995 |
| - (*z).sp = 0xffff as i32 as uint16_t; |
996 |
| - (*z).ix = 0 as i32 as uint16_t; |
997 |
| - (*z).iy = 0 as i32 as uint16_t; |
998 |
| - (*z).mem_ptr = 0 as i32 as uint16_t; |
999 |
| - (*z).c2rust_unnamed.af = 0xffff as i32 as uint16_t; |
1000 |
| - (*z).c2rust_unnamed_0.bc = 0 as i32 as uint16_t; |
1001 |
| - (*z).c2rust_unnamed_1.de = 0 as i32 as uint16_t; |
1002 |
| - (*z).c2rust_unnamed_2.hl = 0 as i32 as uint16_t; |
1003 |
| - (*z).c2rust_unnamed_3.a_f_ = 0 as i32 as uint16_t; |
1004 |
| - (*z).c2rust_unnamed_4.b_c_ = 0 as i32 as uint16_t; |
1005 |
| - (*z).c2rust_unnamed_5.d_e_ = 0 as i32 as uint16_t; |
1006 |
| - (*z).c2rust_unnamed_6.h_l_ = 0 as i32 as uint16_t; |
1007 |
| - (*z).i = 0 as i32 as uint8_t; |
1008 |
| - (*z).r = 0 as i32 as uint8_t; |
1009 |
| - (*z).iff_delay = 0 as i32 as uint8_t; |
1010 |
| - (*z).interrupt_mode = 0 as i32 as uint8_t; |
1011 |
| - (*z).set_iff1(0 as i32 != 0); |
1012 |
| - (*z).set_iff2(0 as i32 != 0); |
1013 |
| - (*z).set_halted(0 as i32 != 0); |
1014 |
| - (*z).irq_pending = 0 as i32 as uint8_t; |
1015 |
| - (*z).nmi_pending = 0 as i32 as uint8_t; |
1016 |
| - (*z).irq_data = 0 as i32 as uint8_t; |
| 970 | + |
| 971 | +pub fn z80_init(z: *mut z80) { |
| 972 | + unsafe { |
| 973 | + (*z).port_in = None; |
| 974 | + (*z).port_out = None; |
| 975 | + (*z).pc = 0 as i32 as uint16_t; |
| 976 | + (*z).sp = 0xffff as i32 as uint16_t; |
| 977 | + (*z).ix = 0 as i32 as uint16_t; |
| 978 | + (*z).iy = 0 as i32 as uint16_t; |
| 979 | + (*z).mem_ptr = 0 as i32 as uint16_t; |
| 980 | + (*z).c2rust_unnamed.af = 0xffff as i32 as uint16_t; |
| 981 | + (*z).c2rust_unnamed_0.bc = 0 as i32 as uint16_t; |
| 982 | + (*z).c2rust_unnamed_1.de = 0 as i32 as uint16_t; |
| 983 | + (*z).c2rust_unnamed_2.hl = 0 as i32 as uint16_t; |
| 984 | + (*z).c2rust_unnamed_3.a_f_ = 0 as i32 as uint16_t; |
| 985 | + (*z).c2rust_unnamed_4.b_c_ = 0 as i32 as uint16_t; |
| 986 | + (*z).c2rust_unnamed_5.d_e_ = 0 as i32 as uint16_t; |
| 987 | + (*z).c2rust_unnamed_6.h_l_ = 0 as i32 as uint16_t; |
| 988 | + (*z).i = 0 as i32 as uint8_t; |
| 989 | + (*z).r = 0 as i32 as uint8_t; |
| 990 | + (*z).iff_delay = 0 as i32 as uint8_t; |
| 991 | + (*z).interrupt_mode = 0 as i32 as uint8_t; |
| 992 | + (*z).set_iff1(0 as i32 != 0); |
| 993 | + (*z).set_iff2(0 as i32 != 0); |
| 994 | + (*z).set_halted(0 as i32 != 0); |
| 995 | + (*z).irq_pending = 0 as i32 as uint8_t; |
| 996 | + (*z).nmi_pending = 0 as i32 as uint8_t; |
| 997 | + (*z).irq_data = 0 as i32 as uint8_t; |
| 998 | + } |
1017 | 999 | }
|
1018 | 1000 | #[no_mangle]
|
1019 | 1001 | pub unsafe extern "C" fn z80_reset(z: *mut z80) {
|
@@ -1101,7 +1083,7 @@ pub unsafe extern "C" fn z80_clr_irq(z: *mut z80) {
|
1101 | 1083 | (*z).irq_pending = 0 as i32 as uint8_t;
|
1102 | 1084 | }
|
1103 | 1085 | unsafe extern "C" fn exec_opcode(z: *mut z80, mut opcode: uint8_t) -> u32 {
|
1104 |
| - let mut cyc: u32 = 0 as i32 as u32; |
| 1086 | + let mut cyc: u32 = 0; |
1105 | 1087 | inc_r(z);
|
1106 | 1088 | match opcode as i32 {
|
1107 | 1089 | 127 => {
|
|
0 commit comments