five32_instruction_set/
fields.rs1use bitvec::field::BitField;
2use bitvec::order::Lsb0;
3use bitvec::slice::BitSlice;
4use bitvec::view::BitView;
5
6pub type Word = u32;
7
8#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
9pub struct ImmediateI(pub u32);
10
11#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
12pub struct ImmediateS(pub u32);
13
14#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
15pub struct ImmediateB(pub u32);
16
17#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
18pub struct ImmediateU(pub u32);
19
20#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
21pub struct ImmediateJ(pub u32);
22
23#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
24pub struct Rd(pub u8);
25
26#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
27pub struct Rs1(pub u8);
28
29#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
30pub struct Rs2(pub u8);
31
32#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
33pub struct Opcode(pub u8);
34
35#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
36pub struct Func3(pub u8);
37
38#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq)]
39pub struct Func7(pub u8);
40
41impl From<&BitSlice<Word, Lsb0>> for Rd {
42 fn from(bs: &BitSlice<Word, Lsb0>) -> Self {
43 assert_eq!(bs.len(), 5);
44 Self(bs.load())
45 }
46}
47
48impl From<&BitSlice<Word, Lsb0>> for Func7 {
49 fn from(bs: &BitSlice<Word, Lsb0>) -> Self {
50 assert_eq!(bs.len(), 7);
51 Self(bs.load())
52 }
53}
54
55impl From<&BitSlice<Word, Lsb0>> for Func3 {
56 fn from(bs: &BitSlice<Word, Lsb0>) -> Self {
57 assert_eq!(bs.len(), 3);
58 Self(bs.load())
59 }
60}
61
62impl From<&BitSlice<Word, Lsb0>> for Opcode {
63 fn from(bs: &BitSlice<Word, Lsb0>) -> Self {
64 assert_eq!(bs.len(), 7);
65 Self(bs.load())
66 }
67}
68
69impl From<&BitSlice<Word, Lsb0>> for Rs1 {
70 fn from(bs: &BitSlice<Word, Lsb0>) -> Self {
71 assert_eq!(bs.len(), 5);
72 Self(bs.load())
73 }
74}
75
76impl From<&BitSlice<Word, Lsb0>> for Rs2 {
77 fn from(bs: &BitSlice<Word, Lsb0>) -> Self {
78 assert_eq!(bs.len(), 5);
79 Self(bs.load())
80 }
81}
82
83impl From<Word> for ImmediateB {
95
96 fn from(w: Word) -> Self {
97 let src = w.view_bits::<Lsb0>();
98 let mut data = 0u32;
99 let dst = data.view_bits_mut::<Lsb0>();
100
101 dst.set(11, src[7]);
102 dst[1..5].copy_from_bitslice(&src[8..=11]);
103 dst[5..11].copy_from_bitslice(&src[25..=30]);
104
105 for i in 12..=31 {
106 dst.set(i, src[31]);
107 }
108
109 Self(data)
110 }
111}
112
113impl From<Word> for ImmediateS {
118
119 fn from(w: Word) -> Self {
120 let src = w.view_bits::<Lsb0>();
121 let mut data = 0u32;
122 let dst = data.view_bits_mut::<Lsb0>();
123
124 dst.set(0, src[7]);
125 dst[1..=4].copy_from_bitslice(&src[8..=11]);
126 dst[5..=10].copy_from_bitslice(&src[25..=30]);
127
128 for i in 11..=31 {
129 dst.set(i, src[31]);
130 }
131
132 Self(data)
133 }
134}
135
136impl From<Word> for ImmediateI {
141
142 fn from(w: Word) -> Self {
143 let src = w.view_bits::<Lsb0>();
144 let mut data = 0u32;
145 let dst = data.view_bits_mut::<Lsb0>();
146
147 dst.set(0, src[20]);
148 dst[1..=4].copy_from_bitslice(&src[21..=24]);
149 dst[5..=10].copy_from_bitslice(&src[25..=30]);
150
151 for i in 11..=31 {
152 dst.set(i, src[31]);
153 }
154
155 Self(data)
156 }
157}
158
159impl From<Word> for ImmediateJ {
164
165 fn from(w: Word) -> Self {
166 let src = w.view_bits::<Lsb0>();
167 let mut data = 0u32;
168 let dst = data.view_bits_mut::<Lsb0>();
169
170 dst[1..5].copy_from_bitslice(&src[21..25]);
171 dst[5..11].copy_from_bitslice(&src[25..31]);
172 dst.set(11, src[20]);
173 dst[12..20].copy_from_bitslice(&src[12..20]);
174
175 for i in 20..32 {
176 dst.set(i, src[31]);
177 }
178
179 Self(data)
180 }
181}
182
183impl From<Word> for ImmediateU {
188 fn from(w: Word) -> Self {
189 let src = w.view_bits::<Lsb0>();
190 let mut data = 0u32;
191 data.view_bits_mut::<Lsb0>()[12..=31].copy_from_bitslice(&src[12..=31]);
192 Self(data)
193 }
194}