Introduce pet-projects dir
[lcapit-junk-code.git] / pet-projects / emu8086 / src / RAMMemory.java
bloba1246558ddec50d216407751113a9bbe677d2de1
1 import java.io.*;
3 /**
4 * Emulates 8086-based computer RAM memory.
5 *
6 * @author Luiz Fernando N. Capitulino
7 */
8 public class RAMMemory implements Serializable {
9 /**
10 * RAM memory contents.
12 private short[] memory;
14 /**
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);
26 /**
27 * Write all memory contents to stdout.
29 public void dump()
31 for (int i = 0; i < memory.length; i++)
32 System.out.printf("%x 0x%x \n", i, memory[i]);
35 /**
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]);
45 /**
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);
58 /**
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));
71 /**
72 * Constructor.
74 * @size RAM size in bytes.
76 public RAMMemory(int size)
78 memory = new short[size];