Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 160 lines (135 sloc) 4.876 kb
f8d1c88 Stuart Walsh Implement a lot of the DMA controller and a sample usage of it from the ...
authored
1 using System;
2 using System.Collections;
05aca8b Stuart Walsh Add a vague ATA implementation, modify CMOS accordingly
authored
3 using System.Runtime.InteropServices;
4 using System.Reflection;
7c48f3e Stuart Walsh Turn off debug output when running not in step mode
authored
5
6 namespace x86CS
7 {
8 public static class Util
9 {
10 public static int CountSet(this BitArray bits)
11 {
12 int count = 0;
13
14 for (int i = 0; i < bits.Length; i++)
15 {
16 if (bits[i])
17 count++;
18 }
19
20 return count;
21 }
3fec5fd Stuart Walsh Fix cpu register access
authored
22
23 public static byte GetLow(this byte b)
24 {
25 return (byte)(b & 0x0f);
26 }
27
28 public static byte SetLow(this byte b, byte value)
29 {
30 return (byte)((b & 0xf0) + (value & 0x0f));
31 }
32
33 public static byte SetHigh(this byte b, byte value)
34 {
35 return (byte)((value.GetLow() << 4) + b.GetLow());
36 }
37
38 public static byte GetHigh(this byte b)
39 {
40 return (byte)((b >> 4) & 0x0f);
41 }
42
43 public static ushort GetLow(this ushort b)
44 {
45 return (ushort)(b & 0x00ff);
46 }
47
48 public static ushort SetLow(this ushort b, ushort value)
49 {
50 return (byte)((b & 0xff00) + (value & 0x00ff));
51 }
52
53 public static ushort SetHigh(this ushort b, ushort value)
54 {
55 return (ushort)((value.GetLow() << 8) + b.GetLow());
56 }
57
58 public static ushort GetHigh(this ushort b)
59 {
0a087e1 Stuart Walsh more opcodes
authored
60 return (ushort)((b >> 8) & 0x00ff);
3fec5fd Stuart Walsh Fix cpu register access
authored
61 }
91c822e Stuart Walsh add some more devices
authored
62
63 public static byte ToBCD(int value)
64 {
65 int tens = value / 10;
66 int ones = value % 10;
67
68 var ret = (byte)(((byte)tens << 4) + (byte)ones);
69
70 return ret;
71 }
05aca8b Stuart Walsh Add a vague ATA implementation, modify CMOS accordingly
authored
72
73 public static T ByteArrayToStructure<T>(byte[] bytes) where T : struct
74 {
75 GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
76
77 T structure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
78 handle.Free();
79
80 return structure;
81 }
82
83 public static T ByteArrayToStructureBigEndian<T>(byte[] bytes) where T : struct
84 {
85 GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
86
87 T structure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
88 handle.Free();
89 System.Type t = structure.GetType();
90 FieldInfo[] fieldInfo = t.GetFields();
91 foreach (FieldInfo fi in fieldInfo)
92 {
93 if (fi.FieldType == typeof(System.Int16))
94 {
95 // TODO
96 }
97 else if (fi.FieldType == typeof(System.Int32))
98 {
99 // TODO
100 }
101 else if (fi.FieldType == typeof(System.Int64))
102 {
103 // TODO
104 }
105 else if (fi.FieldType == typeof(System.UInt16))
106 {
107 UInt16 num = (UInt16)fi.GetValue(structure);
108 byte[] tmp = BitConverter.GetBytes(num);
109 Array.Reverse(tmp);
110 fi.SetValueDirect(__makeref(structure), BitConverter.ToUInt16(tmp, 0));
111 }
112 else if (fi.FieldType == typeof(System.UInt32))
113 {
114 UInt32 num = (UInt32)fi.GetValue(structure);
115 byte[] tmp = BitConverter.GetBytes(num);
116 Array.Reverse(tmp);
117 fi.SetValueDirect(__makeref(structure), BitConverter.ToUInt32(tmp, 0));
118 }
119 else if (fi.FieldType == typeof(System.UInt64))
120 {
121 UInt64 num = (UInt64)fi.GetValue(structure);
122 byte[] tmp = BitConverter.GetBytes(num);
123 Array.Reverse(tmp);
124 fi.SetValueDirect(__makeref(structure), BitConverter.ToUInt64(tmp, 0));
125 }
126 }
127 return structure;
128 }
288fb58 Stuart Walsh Add a Cylinder property which joins the hi/low byte automatically
authored
129
130 public static uint SwapByteOrder(uint source)
131 {
132 uint dest;
133
134 dest = (uint)(((source & 0xff) << 24) |
135 ((source & 0xff00) << 8) |
136 ((source & 0xff0000) >> 8) |
137 ((source & 0xff000000) >> 24));
138
139 return dest;
140 }
141
142 public static void ByteArrayToUShort(byte[] source, ref ushort[] dest, int index)
143 {
144 for (int i = 0, j = index; i < source.Length; i += 2, j++)
145 {
146 dest[j] = (ushort)((source[i + 1] << 8) + source[i]);
147 }
148 }
149
150 public static void UShortArrayToByte(ushort[] source, ref byte[] dest, int index)
151 {
152 for (int i = 0, j = index; j < source.Length; i += 2, j++)
153 {
154 dest[i] = (byte)source[j];
155 dest[i + 1] = (byte)(source[j] << 8);
156 }
157 }
7c48f3e Stuart Walsh Turn off debug output when running not in step mode
authored
158 }
159 }
Something went wrong with that request. Please try again.