4 * Emulates 8086-based computer RAM memory.
6 * @author Luiz Fernando N. Capitulino
8 public class RAMMemory
implements Serializable
{
10 * RAM memory contents.
12 private short[] memory
;
15 * Triggers InvalidMemException if addr is invalid
17 * @param addr memory address to check
18 * @throws InvalidMemException
20 private void invalid_address(int addr
) throws InvalidMemException
22 if ((addr
< 0) || (addr
>= memory
.length
))
23 throw new InvalidMemException(addr
);
27 * Write all memory contents to stdout.
31 for (int i
= 0; i
< memory
.length
; i
++)
32 System
.out
.printf("%x 0x%x \n", i
, memory
[i
]);
36 * Print the contents of the specified address.
38 * @param addr RAM address to print contents
40 public void show(int addr
)
42 System
.out
.printf("[MEM 0x%x] 0x%x\n", addr
, memory
[addr
]);
46 * Write one byte into the specified RAM address.
48 * @param addr RAM address to write to
49 * @param value new contents for the specified address
50 * @throws InvalidMemException
52 public void write(int addr
, short value
) throws InvalidMemException
54 invalid_address(addr
);
55 memory
[addr
] = (short) (value
& (short) 255);
59 * Read one byte from the specified RAM address.
61 * @param addr RAM address to read from
62 * @return address contents
63 * @throws InvalidMemException
65 public short read(int addr
) throws InvalidMemException
67 invalid_address(addr
);
68 return ((short) (memory
[addr
] & 255));
74 * @size RAM size in bytes.
76 public RAMMemory(int size
)
78 memory
= new short[size
];