-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBarrelShifter.cs
84 lines (79 loc) · 3.09 KB
/
BarrelShifter.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace armsimGUI
{
public static class BarrelShifter
{
public static uint rotateVal(uint data, byte shiftType, uint shiftVal)
{
uint shiftedData;
//shiftVal *= 2; // compensate for the instruction compression
switch (shiftType)
{
case 0x00:
shiftedData = LSL(data, (byte)shiftVal);
return shiftedData;
case 0x01:
shiftedData = LSR(data, (byte)shiftVal);
return shiftedData;
case 0x02:
shiftedData = ASR(data, (byte)shiftVal);
return shiftedData;
case 0x03:
shiftedData = ROR(data, (byte)shiftVal);
return shiftedData;
default:
return 0x101; // invalid shifted value, so impossible result; signifies invalid instruction
}
}
//--------------------------------------------------------------
// Purpose: Performs a logical shift left on <data> by <shiftVal> positions
// Returns: nothing
//--------------------------------------------------------------
private static uint LSL(uint data, byte shiftVal)
{
return data << shiftVal;
}
//--------------------------------------------------------------
// Purpose: Performs a logical shift right on <data> by <shiftVal> positions
// Returns: nothing
//--------------------------------------------------------------
private static uint LSR(uint data, byte shiftVal)
{
return data >> shiftVal;
}
//--------------------------------------------------------------
// Purpose: Performs an arithmetic shift right on <data> by <shiftVal> positions
// Returns: nothing
//--------------------------------------------------------------
private static uint ASR(uint data, byte shiftVal)
{
uint MSB = data & 0x80000000; // test MSB
if (MSB == 0x80000000)
{
for (int i = 0; i < shiftVal; i++)
{
data = data >> 1;
data = data | 0x80000000;
}
return data;
}
else return data >> shiftVal;
}
//--------------------------------------------------------------
// Purpose: Performs a rotate right on <data> by <shiftVal> positions
// Returns: nothing
//--------------------------------------------------------------
private static uint ROR(uint data, byte shiftVal)
{
// rotate <data> right <shiftVal> positions
uint rightShift = data >> shiftVal;
// rotate <data> left 32 - <shiftVal> positions
uint leftShift = data << (32 - shiftVal);
// bitwise OR the two together
return (rightShift | leftShift);
}
}
}