7
7
using namespace machine ;
8
8
9
9
static const uint32_t MASK_AMO_RS2 = 0b1111100000000000000000000 ;
10
+ static const uint32_t MASK_I_RS1 = 0b11111000000000000000 ;
10
11
11
- #define AMO_MAP_4ITEMS (NAME_BASE, MASK ) \
12
- { NAME_BASE, MASK }, { NAME_BASE " .rl" , MASK }, { NAME_BASE " .aq" , MASK }, \
13
- { NAME_BASE " .aqrl" , MASK },
12
+ #define AMO_MAP_4ITEMS (NAME, MASK ) \
13
+ { NAME, MASK }, { NAME " .aq" , MASK }, { NAME " .rl" , MASK }, { \
14
+ NAME " .aqrl" , MASK \
15
+ }
14
16
15
- static const QMap<QString, uint32_t > mask_map
16
- = { AMO_MAP_4ITEMS (" lr.w" , MASK_AMO_RS2) AMO_MAP_4ITEMS (" lr.d" , MASK_AMO_RS2) };
17
+ static const QMap<QString, uint32_t > mask_map = {
18
+ // lr.w/d rs2 = 0
19
+ AMO_MAP_4ITEMS (" lr.w" , MASK_AMO_RS2), AMO_MAP_4ITEMS (" lr.d" , MASK_AMO_RS2),
20
+ // csrw(alias of csrrs) rs1 = 0
21
+ // { "csrrs", MASK_I_RS1 }
22
+ };
17
23
18
24
void generate_code_and_string_data (
19
25
QTextStream &out,
@@ -30,7 +36,7 @@ void generate_code_and_string_data(
30
36
}
31
37
const InstructionMap *im = &im_iter[i];
32
38
33
- if (im->name == " unknown" ) { continue ; }
39
+ if (QString ( im->name ) == " unknown" ) { continue ; }
34
40
35
41
if (im->subclass != nullptr ) {
36
42
generate_code_and_string_data (out, im->subclass , im->subfield , nullptr );
@@ -41,7 +47,7 @@ void generate_code_and_string_data(
41
47
QString string_data = im->name ;
42
48
43
49
if (im->args .size ()) {
44
- int val_mask;
50
+ uint32_t val_mask = 0 ;
45
51
Instruction::Type t = im->type ;
46
52
if (alias_parent != nullptr ) { t = alias_parent->type ; }
47
53
switch (t) {
@@ -52,7 +58,8 @@ void generate_code_and_string_data(
52
58
}
53
59
case Instruction::Type::I: val_mask = 0b00000000000100001000000010000000 ; break ;
54
60
case Instruction::Type::ZICSR: {
55
- val_mask = 0b00000000000100001000000010000000 ;
61
+ // dest is not set here
62
+ val_mask = 0b00000000000000001000000010000000 ;
56
63
break ;
57
64
}
58
65
case Instruction::Type::S: {
@@ -108,7 +115,17 @@ void generate_code_and_string_data(
108
115
break ;
109
116
}
110
117
case ' E' : {
111
- string_data += " 0x1" ;
118
+ int i = 0 ;
119
+ for (auto ® : CSR::REGISTERS) {
120
+ if (i < 2 ) {
121
+ i++;
122
+ continue ;
123
+ }
124
+ string_data += QString::asprintf (" 0x%x" , reg.address .data );
125
+ code |= reg.address .data << 20 ;
126
+ if (mask_map.count (im->name )) { code &= ~mask_map[im->name ]; }
127
+ break ;
128
+ }
112
129
break ;
113
130
}
114
131
}
@@ -124,25 +141,53 @@ void generate_code_and_string_data(
124
141
bool check_failed = false ;
125
142
QString parsed_string_data = Instruction (code).to_str ();
126
143
if (alias_parent == nullptr ) {
144
+ // bash inst
127
145
enum_str = QString::asprintf (" {0x%x, \" %s\" }," , code, qPrintable (string_data));
128
146
if (parsed_string_data != string_data) { check_failed = true ; }
129
147
} else {
148
+ // alias inst
130
149
enum_str = QString::asprintf (
131
150
" {0x%x, \" %s\" , \" %s\" }," , code, qPrintable (parsed_string_data),
132
151
qPrintable (string_data));
152
+ uint32_t parsed_code;
153
+ Instruction::code_from_string (
154
+ &parsed_code, parsed_string_data.length (), string_data, Address (0x0 ));
155
+ if (parsed_code != code) { check_failed = true ; }
133
156
}
134
- uint32_t parsed_code;
135
- Instruction::code_from_string (
136
- &parsed_code, parsed_string_data.length (), string_data, Address (0x0 ));
137
- if (parsed_code != code) { check_failed = true ; }
138
157
if (check_failed) { enum_str += " // failed" ; }
139
158
140
159
enum_str += " \n " ;
141
160
out << enum_str;
142
161
}
143
162
}
144
163
145
- int main (int argc, char *argv[]) {
164
+ int main () {
165
+ struct {
166
+ QString str;
167
+ uint32_t code;
168
+ } strings[] = { { " csrrci x0, 0xc00, 0x1" , 0xc000f073 }, { " csrw 0xc00, x1" , 0xc0009073 } };
169
+ for (int i = 0 ; i < 2 ; i++) {
170
+ QString expected_str = strings[i].str ;
171
+ uint32_t expected_code = strings[i].code ;
172
+
173
+ bool check_failed = false ;
174
+ uint32_t parsed_code;
175
+ Instruction::code_from_string (
176
+ &parsed_code, expected_str.length (), expected_str, Address (0x0 ));
177
+ printf (" code: %x " , parsed_code);
178
+ if (parsed_code != expected_code) { check_failed = true ; }
179
+
180
+ QString parsed_string_data = Instruction (parsed_code).to_str ();
181
+ printf (" parsed_string_data: %s " , qPrintable (parsed_string_data));
182
+
183
+ parsed_code = 0 ;
184
+ Instruction::code_from_string (
185
+ &parsed_code, parsed_string_data.length (), parsed_string_data, Address (0x0 ));
186
+ printf (" parsed_code_from_parsed_string_data: %x " , parsed_code);
187
+ if (parsed_code != expected_code) { check_failed = true ; }
188
+ if (check_failed) { printf (" // failed\n " ); }
189
+ }
190
+
146
191
fill_argdesbycode ();
147
192
instruction_from_string_build_base ();
148
193
QFile outfile (" instruction.test.data.h" );
0 commit comments