-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
166 lines (142 loc) · 4.51 KB
/
build.rs
File metadata and controls
166 lines (142 loc) · 4.51 KB
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
//! Generates instruction definitions from the data in the `definitions` subdirectory.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use anyhow::Result;
use quote::quote;
use serde::{Deserialize, Deserializer};
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct Instruction {
#[serde(deserialize_with = "deserialize_hex_literal")]
byte: u8,
mnemonic: String,
cycles: u32,
condition_cycles: Option<u32>,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct PrefixInstruction {
#[serde(deserialize_with = "deserialize_hex_literal")]
byte: u8,
mnemonic: String,
}
fn deserialize_hex_literal<'de, D>(deserializer: D) -> std::result::Result<u8, D::Error>
where
D: Deserializer<'de>,
{
let string = String::deserialize(deserializer)?;
let string = string.trim_start_matches("0x");
u8::from_str_radix(string, 16).map_err(serde::de::Error::custom)
}
fn parse_operands(description: &str) -> u8 {
if description.contains("d8")
|| description.contains("a8")
|| description.contains("r8")
|| description.contains("PREFIX CB")
{
1
} else if description.contains("d16") || description.contains("a16") {
2
} else {
0
}
}
fn parse_prefix_cycles(description: &str) -> u32 {
// If the instruction accesses memory (through HL), the instruction will take 16 cycles.
// Otherwise, it will take 8.
if description.contains("(HL)") {
// However, the BIT instruction is slightly faster.
if description.contains("BIT") {
12
} else {
16
}
} else {
8
}
}
fn write_instructions<P: AsRef<Path>>(filename: P) -> Result<()> {
let instruction_definitions = File::open("definitions/instructions.tsv")?;
let mut instruction_definitions = csv::ReaderBuilder::new()
.delimiter(b'\t')
.from_reader(instruction_definitions);
let mut instructions_out = File::create(filename)?;
writeln!(instructions_out, "[")?;
let mut instructions = vec![];
for result in instruction_definitions.deserialize() {
let instruction: Instruction = result?;
instructions.push(instruction);
}
instructions.sort_unstable_by_key(|i| i.byte);
for instruction in &instructions {
let operands = parse_operands(&instruction.mnemonic);
let Instruction {
ref byte,
ref mnemonic,
ref cycles,
..
} = *instruction;
let condition_cycles = match instruction.condition_cycles {
Some(cycles) => quote! { Some(TCycles(#cycles)) },
None => quote! { None },
};
writeln!(
instructions_out,
"{}",
quote! {
InstructionDef {
byte: #byte,
description: #mnemonic,
num_operands: #operands,
cycles: TCycles(#cycles),
condition_cycles: #condition_cycles,
},
}
)?;
}
write!(instructions_out, "]")?;
Ok(())
}
fn write_prefix_instructions<P: AsRef<Path>>(filename: P) -> Result<()> {
let instruction_definitions = File::open("definitions/prefix.tsv")?;
let mut instruction_definitions = csv::ReaderBuilder::new()
.delimiter(b'\t')
.from_reader(instruction_definitions);
let mut instructions_out = File::create(filename)?;
writeln!(instructions_out, "[")?;
let mut instructions = vec![];
for result in instruction_definitions.deserialize() {
let instruction: PrefixInstruction = result?;
instructions.push(instruction);
}
instructions.sort_unstable_by_key(|i| i.byte);
for instruction in &instructions {
let cycles = parse_prefix_cycles(&instruction.mnemonic);
let PrefixInstruction {
ref byte,
ref mnemonic,
..
} = *instruction;
writeln!(
instructions_out,
"{}",
quote! {
PrefixInstructionDef {
byte: #byte,
description: #mnemonic,
cycles: TCycles(#cycles),
},
}
)?;
}
write!(instructions_out, "]")?;
Ok(())
}
fn main() -> Result<()> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
write_instructions(out_dir.join("instructions.rs"))?;
write_prefix_instructions(out_dir.join("prefix_instructions.rs"))?;
Ok(())
}