-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBus.java
77 lines (69 loc) · 1.58 KB
/
Bus.java
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
import java.nio.*;
/**
* Write a description of class Bus here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bus
{
private byte[] data;
private int busSize;
private int wordSize;
public Bus(int busSize, int wordSize)
{
this.busSize = busSize;
this.wordSize = wordSize;
data = new byte[busSize];
}
/**
* Write to bus (word sized data)
*
* @param stream the data to write
*/
public void write(byte[] stream) {
if (stream.length != busSize) return;
data = stream;
}
/**
* Write to bus (an integer)
*
* @param stream the data to write
*/
public void writeInt(int stream) {
// This method won't work if the bus size is less than int's size
if (busSize < wordSize) return;
data = ByteBuffer.allocate(busSize).putInt(stream).array();
}
/**
* Access bus data (word)
*
* @return data in bus
*/
public byte[] read()
{
return data;
}
/**
* Access bus data (as integer)
*
* @return data in bus
*/
public int readInt()
{
ByteBuffer bb = ByteBuffer.allocate(wordSize).put(data);
return bb.getInt(0);
}
/**
* Access bus data (as a hex string)
*
* @return data in bus
*/
public String readHex() {
StringBuilder sb = new StringBuilder();
for (byte b : data) {
sb.append(String.format("%02X ", b));
}
return sb.toString().trim();
}
}