@@ -17,6 +17,36 @@ use std::iter;
17
17
#[ derive( Debug ) ]
18
18
pub struct RpnExpr ( Vec < u8 > ) ;
19
19
20
+ /// A section memory type.
21
+ #[ derive( Debug , PartialEq ) ]
22
+ pub enum SectType {
23
+ Wram0 ,
24
+ Vram ,
25
+ Romx ,
26
+ Rom0 ,
27
+ Hram ,
28
+ Wramx ,
29
+ Sram ,
30
+ Oam ,
31
+ }
32
+ impl SectType {
33
+ /// The section type's name.
34
+ pub fn name ( & self ) -> & ' static str {
35
+ use SectType :: * ;
36
+
37
+ match self {
38
+ Wram0 => "WRAM0" ,
39
+ Vram => "VRAM" ,
40
+ Romx => "ROMX" ,
41
+ Rom0 => "ROM0" ,
42
+ Hram => "HRAM" ,
43
+ Wramx => "WRAMX" ,
44
+ Sram => "SRAM" ,
45
+ Oam => "OAM" ,
46
+ }
47
+ }
48
+ }
49
+
20
50
impl RpnExpr {
21
51
/// Constructs a RPN expression from its byte serialization.
22
52
/// This does not check the expression's correctness.
@@ -65,6 +95,24 @@ impl<'a> Iter<'a> {
65
95
}
66
96
}
67
97
}
98
+
99
+ fn read_sect_type ( & mut self ) -> Option < SectType > {
100
+ use SectType :: * ;
101
+
102
+ let val = match self . 0 . bytes ( ) . get ( self . 1 ) {
103
+ Some ( 0 ) => Some ( Wram0 ) ,
104
+ Some ( 1 ) => Some ( Vram ) ,
105
+ Some ( 2 ) => Some ( Romx ) ,
106
+ Some ( 3 ) => Some ( Rom0 ) ,
107
+ Some ( 4 ) => Some ( Hram ) ,
108
+ Some ( 5 ) => Some ( Wramx ) ,
109
+ Some ( 6 ) => Some ( Sram ) ,
110
+ Some ( 7 ) => Some ( Oam ) ,
111
+ _ => None ,
112
+ } ;
113
+ self . 1 += 1 ;
114
+ val
115
+ }
68
116
}
69
117
impl < ' a > Iterator for Iter < ' a > {
70
118
type Item = Result < RpnOp < ' a > , RpnIterError > ;
@@ -112,6 +160,12 @@ impl<'a> Iterator for Iter<'a> {
112
160
0x54 => self
113
161
. read_string ( )
114
162
. map_or ( err, |string| Ok ( RpnOp :: StartofSect ( string) ) ) ,
163
+ 0x55 => self
164
+ . read_sect_type ( )
165
+ . map_or ( err, |sect_type| Ok ( RpnOp :: SizeofSectType ( sect_type) ) ) ,
166
+ 0x56 => self
167
+ . read_sect_type ( )
168
+ . map_or ( err, |sect_type| Ok ( RpnOp :: StartofSectType ( sect_type) ) ) ,
115
169
0x60 => Ok ( RpnOp :: HramCheck ) ,
116
170
0x61 => Ok ( RpnOp :: RstCheck ) ,
117
171
0x80 => self . read_u32 ( ) . map_or ( err, |id| Ok ( RpnOp :: Int ( id) ) ) ,
@@ -284,6 +338,10 @@ pub enum RpnOp<'a> {
284
338
SizeofSect ( & ' a [ u8 ] ) ,
285
339
/// `STARTOF("section")`
286
340
StartofSect ( & ' a [ u8 ] ) ,
341
+ /// `SIZEOF(SectionType)`
342
+ SizeofSectType ( SectType ) ,
343
+ /// `STARTOF(SectionType)`
344
+ StartofSectType ( SectType ) ,
287
345
/// HRAM check (check if the value is in HRAM range, then `& 0xFF`).
288
346
HramCheck ,
289
347
/// `rst` check (check if the value is a `rst` target, then `| 0xC7`).
@@ -336,6 +394,8 @@ impl RpnOp<'_> {
336
394
BankSelf => Literal ,
337
395
SizeofSect ( ..) => Literal ,
338
396
StartofSect ( ..) => Literal ,
397
+ SizeofSectType ( ..) => Literal ,
398
+ StartofSectType ( ..) => Literal ,
339
399
HramCheck => Unary ,
340
400
RstCheck => Unary ,
341
401
Int ( ..) => Literal ,
@@ -363,7 +423,8 @@ impl RpnOp<'_> {
363
423
364
424
// There is no precedence for non-binary operators...
365
425
Neg | Cpl | Not | BankSym ( ..) | BankSect ( ..) | BankSelf | SizeofSect ( ..)
366
- | StartofSect ( ..) | HramCheck | RstCheck | Int ( ..) | Sym ( ..) => unreachable ! ( ) ,
426
+ | StartofSect ( ..) | SizeofSectType ( ..) | StartofSectType ( ..) | HramCheck | RstCheck
427
+ | Int ( ..) | Sym ( ..) => unreachable ! ( ) ,
367
428
}
368
429
}
369
430
@@ -381,7 +442,8 @@ impl RpnOp<'_> {
381
442
382
443
// There is no associativity for non-binary operators...
383
444
Neg | Cpl | Not | BankSym ( ..) | BankSect ( ..) | BankSelf | SizeofSect ( ..)
384
- | StartofSect ( ..) | HramCheck | RstCheck | Int ( ..) | Sym ( ..) => unreachable ! ( ) ,
445
+ | StartofSect ( ..) | SizeofSectType ( ..) | StartofSectType ( ..) | HramCheck | RstCheck
446
+ | Int ( ..) | Sym ( ..) => unreachable ! ( ) ,
385
447
}
386
448
}
387
449
@@ -444,6 +506,8 @@ impl Display for RpnOp<'_> {
444
506
BankSelf => write ! ( fmt, "BANK(@)" ) ,
445
507
SizeofSect ( name) => write ! ( fmt, "SIZEOF(\" {}\" )" , String :: from_utf8_lossy( & name) ) ,
446
508
StartofSect ( name) => write ! ( fmt, "STARTOF(\" {}\" )" , String :: from_utf8_lossy( & name) ) ,
509
+ SizeofSectType ( sect_type) => write ! ( fmt, "SIZEOF({})" , sect_type. name( ) ) ,
510
+ StartofSectType ( sect_type) => write ! ( fmt, "STARTOF({})" , sect_type. name( ) ) ,
447
511
HramCheck => write ! ( fmt, "HRAM?" ) ,
448
512
RstCheck => write ! ( fmt, "RST?" ) ,
449
513
Int ( val) => write ! ( fmt, "${:04x}" , val) ,
0 commit comments