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
|
pub mod audio;
pub mod consts;
pub mod desktop;
pub mod display;
pub mod interrupts_timers;
pub mod io;
pub mod mmio;
pub mod opcodes;
pub mod state;
pub mod logs;
use crate::logs::{log, LogLevel};
use crate::desktop::input::{Gamepad, GamepadRecorder, GamepadReplay, Keyboard};
use crate::desktop::load_save::FSLoadSave;
use crate::io::{Serial, Input};
use clap::Parser;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// The gameboy rom file
rom: String,
/// Serial communication input from a FIFO file
#[arg(long)]
fifo_input: Option<String>,
/// Serial communication output from a FIFO file
#[arg(long)]
fifo_output: Option<String>,
/// Record the inputs into a file when they happen so it can be replayed with --replay-input
#[arg(long)]
record_input: Option<String>,
/// Replay the inputs from the file recorded by record_input
#[arg(long)]
replay_input: Option<String>,
/// The file to which the state will be save when using the X (North) button of the controller
/// and loaded from when using the --load-state parameter
#[arg(long)]
state_file: Option<String>,
/// Start at the state defined by --state-file instead of the start of bootrom with empty mem
#[arg(short, long, default_value_t = false)]
load_state: bool,
/// Gets inputs from keyboard instead of gamepad
#[arg(short, long, default_value_t = false)]
keyboard: bool,
#[arg(short, long, default_value_t = 1.0)]
speed: f32,
/// Skip bootrom (will start the execution at 0x100 with all registers empty
#[arg(long, default_value_t = false)]
skip_bootrom: bool,
/// Dump state to files on stop
#[arg(long, default_value_t = false)]
stop_dump_state: bool,
/// Window title
#[arg(long, default_value = "Gameboy Emulator")]
title: String,
/// Serial tcp listen port
#[arg(short='L', long)]
listen: Option<u16>,
/// Serial tcp connect address <address:port>
#[arg(short, long)]
connect: Option<String>,
/// Don't send (or expect) a byte as a response to a serial transfer
#[arg(long, default_value_t = false)]
no_response: bool,
/// Verbosity. Coma separated values (possible values: infos,debug,opcode_dump,halt_cycles,errors,none)
#[arg(short, long, default_value = "infos,errors")]
verbosity: String,
}
fn main() {
let cli = Cli::parse();
logs::set_log_level(cli.verbosity);
log(LogLevel::Infos, format!("Starting {:?}...", &cli.rom));
let window = desktop::window::DesktopWindow::new(cli.title).unwrap();
let serial: Box<dyn Serial> = match (cli.fifo_input, cli.fifo_output, cli.listen, cli.connect) {
(_, _, Some(port), _) => Box::new(desktop::serial::TcpSerial::new_listener(port, cli.no_response)),
(_, _, _, Some(addr)) => Box::new(desktop::serial::TcpSerial::connect(addr, cli.no_response)),
(Some(fifo_input), Some(fifo_output), _, _) => Box::new(desktop::serial::FIFOSerial::new(fifo_input, fifo_output, cli.no_response)),
_ => Box::new(desktop::serial::UnconnectedSerial {})
};
let mut gamepad: Box<dyn Input> = if let Some(record_file) = cli.replay_input {
Box::new(GamepadReplay::new(record_file))
} else if cli.keyboard {
Box::new(Keyboard::new(window.keys.clone()))
} else {
Box::new(Gamepad::new())
};
if let Some(record_file) = cli.record_input {
gamepad = Box::new(GamepadRecorder::new(gamepad, record_file));
};
let mut fs_load_save = FSLoadSave::new(&cli.rom, format!("{}.sav", &cli.rom));
if let Some(state_file) = &cli.state_file {
fs_load_save = fs_load_save.state_file(state_file);
}
let mut gameboy = io::Gameboy::<_, _, _, desktop::audio::RodioAudio, _>::new(
gamepad,
window,
serial,
fs_load_save,
cli.speed as f64,
);
if cli.load_state {
gameboy.load_state().unwrap();
}
if cli.skip_bootrom {
gameboy.skip_bootrom();
}
gameboy.start();
if cli.stop_dump_state {
gameboy.dump_state().unwrap();
}
}
|