1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#![allow(clippy::new_without_default)]
#![allow(clippy::unusual_byte_groupings)]
use std::{
collections::HashMap,
sync::mpsc::TryRecvError,
time::{Duration, Instant},
};
use armv4t_emu::{reg, Cpu, Exception, Memory, Mode};
use crate::mem::{Ctsi, Event, GenSIO, GensioState, Mem, Uif};
mod currentcontroller;
mod mem;
use currentcontroller::CurrentController;
fn main() {
simple_logger::SimpleLogger::new()
.with_level(log::LevelFilter::Info)
.with_module_level("nokia3310emu::currentcontroller", log::LevelFilter::Trace)
//.with_module_level("pc_all", log::LevelFilter::Trace)
.with_module_level("eec46_jmp", log::LevelFilter::Trace)
.with_module_level("armv4t_emu", log::LevelFilter::Info)
.env()
.with_colors(true)
.init()
.unwrap();
// Channel so the memory controller can tell us when memory mapped
// IO is activated
let (mut tx, mut rx) = std::sync::mpsc::sync_channel::<Event>(10);
// Setup Memory
let mut memory_array = vec![0; 0x800000];
let file = std::fs::read("My 3310 NR1 v5.79.fls").unwrap();
//let file = std::fs::read("FuBu3310 v6.39 (PPM B).fls").unwrap();
memory_array[0x00200000..0x003FFFFF + 1].copy_from_slice(&file);
let mut mem = Mem {
inner: &mut memory_array,
tx,
};
// Current Controller (CCont)
let mut ccont = CurrentController::new();
// Setup CPU
let mut cpu = Cpu::new();
// We don't have a bootloader. Hope everything is okay and jump straight
// to where the bootloader does
cpu.reg_set(Mode::User, reg::PC, 0x200040);
let mut hit_eec46 = false;
// Main loop
let mut last_pc = 0x0;
let mut print_all_pc = false;
let mut jump_counter = JumpCounter::default();
let known_jumps = vec![
0x002ef9e6, // Delay_r0_loop
];
let start = Instant::now();
let run_duration = Duration::from_millis(5000);
'cpustep: while cpu.step(&mut mem) {
// Log jumps to make debugging easier. We can look at these
// addresses in Ghidra
let pc = cpu.reg_get(Mode::User, reg::PC);
if print_all_pc {
log::trace!(target: "pc_all", "[{}] {pc:08x}", pc as isize - last_pc as isize);
}
if (pc as isize - last_pc as isize).abs() > 4 {
log::trace!("jump detect - {last_pc:08X} => {pc:08X}",);
let count = jump_counter.jump(last_pc, pc);
if count >= 100 && count % 100 == 0 && !known_jumps.contains(&pc) {
log::trace!(target: "eec46_jmp", "[{count:<6}] {last_pc:08x} => {pc:08x}");
}
}
if pc == 0x002eebec && last_pc != 0x002eebec {
log::info!("Entered eebec loop!");
}
if pc == 0x002eec44 {
//let r5 = cpu.reg_get(Mode::User, 5);
// We set R5 here because the next two instructions are as follows:
// [MainLoop?] eec46: cmp r5, 0x1
// eec48: bne [MainLoop?]
// So if we do not set r5 to 0x1, we'll loop back to eec46 forever
log::warn!("hit eec44... setting register 5!");
cpu.reg_set(Mode::User, 5, 0x1);
}
if pc == 0x002eec46 && !hit_eec46 {
hit_eec46 = true;
print_all_pc = true;
log::info!("hit eec46!");
} else if pc != 0x002eec48 && pc != 0x002eec46 && hit_eec46 {
hit_eec46 = false;
print_all_pc = true;
log::warn!("broke from eec46! (pc = {pc:x})");
}
if last_pc == 0x002e8c0e {
//log::error!("right after e8c0e // pc = {pc:08x}");
//break 'cpustep;
}
last_pc = pc;
if pc == 0x002eec52 {
log::info!("hit eec52!");
}
if pc == 0x002e8c0e {
log::warn!("hit e8c0e! setting r0 = 0x81");
cpu.reg_set(Mode::User, 0, 0x81);
}
match rx.try_recv() {
Err(TryRecvError::Empty) => (),
Err(TryRecvError::Disconnected) => panic!("event sender disconnected"),
Ok(event) => match event {
Event::GenSIO(GenSIO::StartTransaction) => {
let start_byte = mem.inner[Mem::GENSIO_START_TRANSACTION];
if start_byte == 0x25 {
mem.gensio_status_clear();
mem.set_gensio_status(GensioState::DataWrite);
mem.set_gensio_status(GensioState::TransactionReady);
}
}
Event::GenSIO(GenSIO::CContWrite) => ccont.event(&mut mem),
Event::GenSIO(GenSIO::LcdCtrl) => {
let dog = mem.inner[Mem::GENSIO_LCD_CTRL];
log::info!("GENSIO LCD Mystery {dog:02X} -- {dog:08b}");
//mem.inner[Mem::UIF_CTRL_IO_2] = 0b0010_0000;
}
Event::Ctsi(Ctsi::AsicWatchdog) => {
let dog = mem.inner[Mem::CTSI_ASIC_WATCHDOG];
log::info!("CTSI Clock ={dog}");
}
Event::Ctsi(Ctsi::WriteClockControl) => {
let clock = mem.inner[Mem::CTSI_CLOCK_CONTROL];
log::info!("CTSI Clock {clock:08b}");
}
Event::Uif(Uif::CtrlIo2) => {
let ctrl = mem.inner[Mem::UIF_CTRL_IO_2];
log::info!("CTSI Clock {ctrl:08b}");
}
},
}
if start.elapsed() >= run_duration {
log::error!("hit run_duration max runtime. breaking from cpustep loop");
break 'cpustep;
}
// Run slow
std::thread::sleep(Duration::from_micros(10));
}
println!("cpu halted");
}
#[derive(Debug, Default)]
pub struct JumpCounter {
jmps: Vec<Jump>,
}
impl JumpCounter {
pub fn jump(&mut self, from: u32, to: u32) -> usize {
match self.jmps.iter_mut().find(|j| j.from == from && j.to == to) {
None => {
self.jmps.push(Jump::new(from, to));
1
}
Some(jmp) => {
jmp.count += 1;
jmp.count
}
}
}
}
#[derive(Debug)]
pub struct Jump {
from: u32,
to: u32,
count: usize,
}
impl Jump {
pub fn new(from: u32, to: u32) -> Self {
Self { from, to, count: 1 }
}
}
pub struct Lcd {
pub data: [u8; 44 * 84],
}
|