From 4ad2150a24682034b550e66620cb549a05e9b50a Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Sun, 17 Mar 2019 19:06:29 +0300 Subject: [PATCH 1/2] Add fcsr register --- asm.S | 2 + src/register/fcsr.rs | 131 +++++++++++++++++++++++++++++++++++++++++++ src/register/mod.rs | 2 + 3 files changed, 135 insertions(+) create mode 100644 src/register/fcsr.rs diff --git a/asm.S b/asm.S index 030dfd1..89890be 100644 --- a/asm.S +++ b/asm.S @@ -24,6 +24,8 @@ __sfence_vma: sfence.vma a0, a1 ret +REG_READ_WRITE(fcsr, 0x003) +REG_SET_CLEAR(fcsr, 0x003) // M-mode registers REG_READ(mcause, 0x342) diff --git a/src/register/fcsr.rs b/src/register/fcsr.rs new file mode 100644 index 0000000..a6b2b13 --- /dev/null +++ b/src/register/fcsr.rs @@ -0,0 +1,131 @@ +//! Floating-point control and status register + +use bit_field::BitField; + +/// Floating-point control and status register +#[derive(Clone, Copy, Debug)] +pub struct FCSR { + bits: u32, +} + +/// Accrued Exception Flags +#[derive(Clone, Copy, Debug)] +pub struct Flags(u32); + +/// Accrued Exception Flag +#[derive(Clone, Copy, Debug)] +pub enum Flag { + /// Inexact + NX = 0b00001, + + /// Underflow + UF = 0b00010, + + /// Overflow + OF = 0b00100, + + /// Divide by Zero + DZ = 0b01000, + + /// Invalid Operation + NV = 0b10000, +} + +impl Flags { + /// Inexact + #[inline] + pub fn nx(&self) -> bool { + self.0.get_bit(0) + } + + /// Underflow + #[inline] + pub fn uf(&self) -> bool { + self.0.get_bit(1) + } + + /// Overflow + #[inline] + pub fn of(&self) -> bool { + self.0.get_bit(2) + } + + /// Divide by Zero + #[inline] + pub fn dz(&self) -> bool { + self.0.get_bit(3) + } + + /// Invalid Operation + #[inline] + pub fn nv(&self) -> bool { + self.0.get_bit(4) + } +} + +/// Rounding Mode +pub enum RoundingMode { + RoundToNearestEven = 0b000, + RoundTowardsZero = 0b001, + RoundDown = 0b010, + RoundUp = 0b011, + RoundToNearestMaxMagnitude = 0b100, + Invalid = 0b111, +} + +impl FCSR { + /// Returns the contents of the register as raw bits + pub fn bits(&self) -> u32 { + self.bits + } + + /// Accrued Exception Flags + #[inline] + pub fn fflags(&self) -> Flags { + Flags(self.bits.get_bits(0..5)) + } + + /// Rounding Mode + #[inline] + pub fn frm(&self) -> RoundingMode { + match self.bits.get_bits(5..8) { + 0b000 => RoundingMode::RoundToNearestEven, + 0b001 => RoundingMode::RoundTowardsZero, + 0b010 => RoundingMode::RoundDown, + 0b011 => RoundingMode::RoundUp, + 0b100 => RoundingMode::RoundToNearestMaxMagnitude, + _ => RoundingMode::Invalid, + } + } +} + +read_csr!(0x003, __read_fcsr); +write_csr!(0x003, __write_fcsr); +clear!(0x003, __clear_fcsr); + +/// Reads the CSR +#[inline] +pub fn read() -> FCSR { + FCSR { bits: unsafe{ _read() as u32 } } +} + +/// Writes the CSR +#[inline] +pub unsafe fn set_rounding_mode(frm: RoundingMode) { + let old = read(); + let bits = ((frm as u32) << 5) | old.fflags().0; + _write(bits as usize); +} + +/// Resets `fflags` field bits +#[inline] +pub unsafe fn clear_flags() { + let mask = 0b11111; + _clear(mask); +} + +/// Resets `fflags` field bit +#[inline] +pub unsafe fn clear_flag(flag: Flag) { + _clear(flag as usize); +} diff --git a/src/register/mod.rs b/src/register/mod.rs index 47914b8..0c38e38 100644 --- a/src/register/mod.rs +++ b/src/register/mod.rs @@ -13,6 +13,8 @@ #[macro_use] mod macros; +pub mod fcsr; + pub mod mcause; pub mod mcycle; pub mod mcycleh; From 8222812d8d8de33ddfe8a66e36366df9c487bc63 Mon Sep 17 00:00:00 2001 From: Vadim Kaushan Date: Sun, 17 Mar 2019 19:06:48 +0300 Subject: [PATCH 2/2] Regenerate blobs --- bin/riscv32imac-unknown-none-elf.a | Bin 6186 -> 6686 bytes bin/riscv32imc-unknown-none-elf.a | Bin 6186 -> 6686 bytes bin/riscv64gc-unknown-none-elf.a | Bin 6980 -> 7638 bytes bin/riscv64imac-unknown-none-elf.a | Bin 6980 -> 7638 bytes 4 files changed, 0 insertions(+), 0 deletions(-) diff --git a/bin/riscv32imac-unknown-none-elf.a b/bin/riscv32imac-unknown-none-elf.a index 2346b0c7c353b2d5da3e59c875ce2f09ccd08a42..2bb3df2d87b74c8b861770f61bbd46d157bd6a15 100644 GIT binary patch delta 1407 zcmchWUr1AN6vw~6U0S+jUAOD3W;IspPG@u3hSrF(9(oWmdkPF@jYZL)2e&YgypbZu_}J%tfLU{DA_)PoTO20rvw5}mtwhez zO^&o%ZK?K9dG&e#Gy~e6|F}E;@v^`)P~JDbB^ys=^L@x<`uolla+yWR=L*g-doFjP zuOvl7eO2_+wNu-jYNfaq-T%Q0D1PxIZy<^UTHqSFdL@q|x=r|OPS}hjz|>PVLnS6s zC=P1Jw0)(A7onkXsX{~3GLJ3uW}0z#cXK5wOGh;hRSeMK!BOE}WQ4E5dEqq}p#Ie@ z)?E>;#Y5pfObYj7O1Ocy!t3x^cmUsp*JDBWdJENdbr+Y84X78s5mDhmB!xGiL-;0i z3Ezw!;ahN8IE_{Ltji)4#<1|MxG8)a#)L;OA$&Vt3ODgicoeh38(T3i0!>&D9)ri_ zRLK6+JcEDR)RT(5-obRK_2Y%yr9xaxgA=OEgcDdY83W#NS248qfKo%0U1zG>be%a) zMdma5$ebokH`Oe~^nO)G5BO}-8|Gg6!8}aCT2uAXLFPp|%N!L|P5xfP3VD%oo~LqIm^ux!#%|}{RY07t-a(cTbYJ6t U6L8UeBkUfn0@}3VYvZK$2f*RQK>z>% delta 993 zcmchVO-NKx6vzMf-D;Z2Vcxv?>QlqgH<@Y1G$sg1n;-&hGr618EJQFUXxhY!DB1)G ze<9o$wP;a9#X};J+5`pGvhTV_Cq+4kbynNjtW} znLK;u1P0uQ@FLE7;Y&McIT0H;(EIyj9WLV7r#yj*@>Lj8-hgrCNz|BH*%{2JX&Seb zdw8HcgL&myyjGsWXXSY;DlfoBOaCbss{^=dum+9F*P>1NI&>>vk3GsaV88N43@UHJ zh;sJEz2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& zzbW@6({zL`qwFaCk!dzUzhlNx`Cm*`gxV5wPEM!jfm1?9a37Ya^a#^Jgzm)}(SuBj z5qcE;Nu^IQ?T^q3jwQ?TGV0`joxzB6A5V!cFr`b>7O${MbeZW;8x!Iay1NvvcI}~$ zydZIVYb~gB2Ud{ju`GE7Dr595N}_DBsV(^|%IP8b7=FgcMTBI&i!m><`2}8fNh+wE zkz??3Mo&`NjVj+{e;Of~=b)6)wHWgen>XMqBOf9p^F@rg$mR;XUL#lG_Zqz(W4>q8 zhSEl+;c25w*2d@j%BG7@*L5YvS!bsTKX2qUgn5&H5My>ojHh?V=kN+fzJ*^f`g4q$ s!`gO}9ca8eisPG`;|LAII8+>UE_60mzDZPzu5+^Sd!hJgq1w~;2T|Ld2><{9 diff --git a/bin/riscv64imac-unknown-none-elf.a b/bin/riscv64imac-unknown-none-elf.a index 124ca149bdf859de31a68937e7f98d1d6dc7d8bc..a64094439cd34e7302eb5809e0aedf51d61d76a7 100644 GIT binary patch delta 1311 zcmciAOK1~87zgluWK5e-*|<%%4Xd;s?52;nl|F)@XfNtTtG#w>T1upX6>K94QtIo{ zgP?pW3Q-XY_Rvc$f(nAc;!Q0>k&?noRNnHX0xnN#CObJSa9Z6-V^x;ei#Hk+5vJNtx8RI)FFZys;WN@Fgh^{vu9~N| zYD>(@>tHbr49v6b()rN_p0nx^SV?~)@0uDM;<~Y7)mP*g!lZ_(ROKRdHBg(_H-`!TRl`Fu8K43^4c{=rDdxr tP_de%!T7Fd3Og+}qpCC}F|^Tqyd|`+x|GB;T8(#w4u?zZ)@jw)@)O131Uvu$ delta 1150 zcmchXJ!lj`6vtpyk}C*(5;%bu`zPPlOGadl)1otHvVn`}&|9}}af z**ciF?{#3lv#Jl!a!Mo^w{}x738kl*CR+i|&tR75d8U7NX>l&$ZsM@M!*pBAdUN^& zzbW@6({zL`qwFaCk!dzUzhlNx`Cm*`gxV5wPEM!jfm1?9a37Ya^a#^Jgzm)}(SuBj z5qcE;Nu^IQ?T^q3jwQ?TGV0`joxzB6A5V!cFr`b>7O${MbeZW;8x!Iay1NvvcI}~$ zydZIVYb~gB2Ud{ju`GE7Dr595N}_DBsV(^|%IP8b7=FgcMTBI&i!m><`2}8fNh+wE zkz??3Mo&`NjVj+{e;Of~=b)6)wHWgen>XMqBOf9p^F@rg$mR;XUL#lG_Zqz(W4>q8 zhSEl+;c25w*2d@j%BG7@*L5YvS!bsTKX2qUgn5&H5My>ojHh?V=kN+fzJ*^f`g4q$ s!`gO}9ca8eisPG`;|LAII8+>UE_60mzDZPzu5+^Sd!hJgq1w~;2T|Ld2><{9