6 /** Implements a Z80 with 64k RAM and a write-only console at IO port 0.
7 @author Michael Hope <michaelh@juju.net.nz> 2001
10 public class ConsoleZ80
14 String romImageName
= null;
18 public ConsoleZ80(String _romImageName
) {
20 romImageName
= _romImageName
;
21 isWatched
= new boolean[65536];
28 public void setImageName(String name
) {
33 public int peekb( int addr
) {
34 // System.out.println("Read from " + addr + " of " + mem[addr]);
38 public void pokeb( int addr
, int newByte
) {
39 if (isWatched
[addr
]) {
40 System
.out
.println("watch: write to 0x" + Integer
.toString(addr
, 16) + " at pc 0x" + Integer
.toString(PC(), 16));
42 mem
[ addr
] = newByte
;
45 public int inb( int port
) {
49 public void outb( int port
, int outByte
, int tstates
) {
52 System
.out
.write(outByte
);
64 public void loadROM( String name
, InputStream is
) throws Exception
{
65 readBytes(is
, mem
, 32768);
75 System
.out
.write(L());
84 return super.rst(num
);
88 private int readBytes( InputStream is
, int a
[], int n
) throws Exception
{
90 byte buff
[] = new byte[ n
];
92 while ( toRead
> 0 ) {
93 int nRead
= is
.read(buff
, n
-toRead
, toRead
);
96 for ( int i
= 0; i
< n
; i
++ ) {
97 a
[i
] = (buff
[i
]+256)&0xff;
102 catch ( Exception e
) {
107 /** Start the applet creating a new thread which invokes run(). */
108 public void start() throws Exception
{
109 Thread thread
= new Thread(this, "ConsoleZ80");
113 /** Read applet parameters and start the Rex. */
116 FileInputStream rom
= new FileInputStream(romImageName
);
117 loadROM(romImageName
, rom
);
121 } catch (Exception e
) {
122 System
.out
.println("readParameters: " +e
.toString());
126 public void addWatch(String addr
) {
128 isWatched
[Integer
.valueOf(addr
).intValue()] = true;
129 } catch (NumberFormatException ignored
) {};
132 public static void main(String
[] args
) throws Exception
{
133 boolean setImage
= false;
134 ConsoleZ80 z80
= new ConsoleZ80();
136 for (int i
=0; i
<args
.length
; i
++) {
137 if (args
[i
].equals("-w")) {
138 z80
.addWatch(args
[++i
]);
140 else if (args
[i
].equals("-h")) {
141 System
.out
.println("Usage: java ConsoleZ80 [romimage]");
145 z80
.setImageName(args
[i
]);
150 z80
.setImageName("test.gb");