aboutsummaryrefslogtreecommitdiff
path: root/lib/xixanta/src/mapping.rs
blob: 657a94bf2a2908cf587ed887c1a6fa00f753695d (plain)
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
use crate::assembler::Bundle;

lazy_static! {
    pub static ref EMPTY: Vec<Segment> = vec![Segment {
        name: String::from("CODE"),
        start: 0x0000,
        size: 0xFFFF,
        offset: 0,
        fill: None,
        bundles: vec![],
    }];
    pub static ref NROM: Vec<Segment> = vec![
        Segment {
            name: String::from("HEADER"),
            start: 0x0000,
            size: 0x0010,
            offset: 0,
            fill: Some(0x00),
            bundles: vec![],
        },
        Segment {
            name: String::from("VECTORS"),
            start: 0xFFFA,
            size: 0x0006,
            offset: 0,
            fill: Some(0x00),
            bundles: vec![],
        },
        Segment {
            name: String::from("CODE"),
            start: 0x8000,
            size: 0x7FFA,
            offset: 0,
            fill: Some(0x00),
            bundles: vec![],
        },
        Segment {
            name: String::from("CHARS"),
            start: 0x0000,
            size: 0x2000,
            offset: 0,
            fill: Some(0x00),
            bundles: vec![],
        }
    ];
    pub static ref NROM65: Vec<Segment> = vec![
        Segment {
            name: String::from("HEADER"),
            start: 0x0000,
            size: 0x0010,
            offset: 0,
            fill: Some(0x00),
            bundles: vec![],
        },
        Segment {
            name: String::from("VECTORS"),
            start: 0xFFFA,
            size: 0x0006,
            offset: 0,
            fill: Some(0x00),
            bundles: vec![],
        },
        Segment {
            name: String::from("STARTUP"),
            start: 0x8000,
            size: 0x7FFA,
            offset: 0,
            fill: Some(0x00),
            bundles: vec![],
        },
        Segment {
            name: String::from("CODE"),
            start: 0x8000,
            size: 0x7FFA,
            offset: 0,
            fill: Some(0x00),
            bundles: vec![],
        },
        Segment {
            name: String::from("CHARS"),
            start: 0x0000,
            size: 0x2000,
            offset: 0,
            fill: Some(0x00),
            bundles: vec![],
        }
    ];
}

#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub struct Segment {
    pub name: String,
    pub start: u16,
    pub size: usize,
    pub offset: usize,
    pub fill: Option<u8>,
    pub bundles: Vec<Bundle>,
}

/* TODO
#[derive(Debug)]
pub struct Mapping {
    pub segments: Vec<Segment>,
    pub nodes: HashMap<String, Vec<Node>>,
    pub current: String,
    pub macros: HashMap<String, Vec<Node>>,
    pub current_macro: Option<String>,
}

impl Mapping {
    pub fn new(mut segments: Vec<Segment>) -> Self {
        segments.sort_by(|a, b| a.start.cmp(&b.start));

        let mut nodes = HashMap::new();
        for segment in segments.iter() {
            nodes.insert(segment.name.clone(), vec![]);
        }

        let current_segment = &segments.first().unwrap().name.clone();

        Mapping {
            segments,
            nodes,
            current: current_segment.to_string(),
            macros: HashMap::new(),
            current_macro: None,
        }
    }

    pub fn reset(&mut self) {
        self.nodes = HashMap::new();
        for segment in self.segments.iter() {
            self.nodes.insert(segment.name.clone(), vec![]);
        }

        self.current = self.segments.first().unwrap().name.clone();

        self.macros = HashMap::new();
        self.current_macro = None;
    }

    pub fn switch(&mut self, id: &PString) -> Result<()> {
        if !self.nodes.contains_key(&id.value) {
            // TODO
            // return Err(
            //     id.parser_error(format!("segment '{}' has not been defined", id.value).as_str())
            // );
        }

        id.value.clone_into(&mut self.current);
        Ok(())
    }

    pub fn current(&self) -> &Vec<Node> {
        self.nodes.get(&self.current).unwrap()
    }

    pub fn current_mut(&mut self) -> &mut Vec<Node> {
        self.nodes.get_mut(&self.current).unwrap()
    }

    pub fn push(&mut self, node: Node) {
        match &self.current_macro {
            Some(m) => self.macros.get_mut(m).unwrap().push(node),
            None => self.nodes.get_mut(&self.current).unwrap().push(node),
        }
    }
}
*/