struct / union in initializer, RFE #901.
[sdcc.git] / sdcc-extra / emu / rose / ConsoleZ80.java
blob96001dd9ec254f4dc69f067fc8956c0542fdf7bc
1 import java.awt.*;
2 import java.util.*;
3 import java.io.*;
4 import java.net.*;
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
8 @see Z80
9 */
10 public class ConsoleZ80
11 extends Z80
12 implements Runnable
14 String romImageName = null;
16 boolean isWatched[];
18 public ConsoleZ80(String _romImageName) {
19 super(4.0, 1);
20 romImageName = _romImageName;
21 isWatched = new boolean[65536];
24 public ConsoleZ80() {
25 this(null);
28 public void setImageName(String name) {
29 romImageName = name;
32 /** Byte access */
33 public int peekb( int addr ) {
34 // System.out.println("Read from " + addr + " of " + mem[addr]);
35 return 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 ) {
46 return 0xff;
49 public void outb( int port, int outByte, int tstates ) {
50 switch (port) {
51 case 0xFF:
52 System.out.write(outByte);
53 System.out.flush();
54 break;
55 case 1:
56 switch (outByte) {
57 case 0:
58 System.exit(0);
59 break;
64 public void loadROM( String name, InputStream is ) throws Exception {
65 readBytes(is, mem, 32768);
68 int rst(int num) {
69 if (num == 8) {
70 switch (A()) {
71 case 0:
72 System.exit(0);
73 break;
74 case 1:
75 System.out.write(L());
76 System.out.flush();
77 break;
78 default:
79 // Drop.
81 return 0;
83 else {
84 return super.rst(num);
88 private int readBytes( InputStream is, int a[], int n ) throws Exception {
89 try {
90 byte buff[] = new byte[ n ];
91 int toRead = n;
92 while ( toRead > 0 ) {
93 int nRead = is.read(buff, n-toRead, toRead);
94 toRead -= nRead;
96 for ( int i = 0; i < n; i++ ) {
97 a[i] = (buff[i]+256)&0xff;
100 return n;
102 catch ( Exception e ) {
103 return -1;
107 /** Start the applet creating a new thread which invokes run(). */
108 public void start() throws Exception {
109 Thread thread = new Thread(this, "ConsoleZ80");
110 thread.start();
113 /** Read applet parameters and start the Rex. */
114 public void run() {
115 try {
116 FileInputStream rom = new FileInputStream(romImageName);
117 loadROM(romImageName, rom);
118 rom.close();
119 reset();
120 execute();
121 } catch (Exception e) {
122 System.out.println("readParameters: " +e.toString());
126 public void addWatch(String addr) {
127 try {
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]");
142 System.exit(0);
144 else {
145 z80.setImageName(args[i]);
146 setImage = true;
149 if (!setImage) {
150 z80.setImageName("test.gb");
152 z80.start();