Working generic word width
This commit is contained in:
		
							parent
							
								
									1dab9e69c7
								
							
						
					
					
						commit
						04840349a9
					
				
					 11 changed files with 143 additions and 48 deletions
				
			
		|  | @ -34,7 +34,7 @@ java { | ||||||
| 
 | 
 | ||||||
| application { | application { | ||||||
|     // Define the main class for the application. |     // Define the main class for the application. | ||||||
|     mainClass.set("hatelace.App") |     mainClass.set("hatelace.Main") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| tasks.named<Test>("test") { | tasks.named<Test>("test") { | ||||||
|  |  | ||||||
							
								
								
									
										13
									
								
								app/src/main/java/hatelace/Address.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								app/src/main/java/hatelace/Address.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,13 @@ | ||||||
|  | package hatelace; | ||||||
|  | 
 | ||||||
|  | public class Address { | ||||||
|  |     int address; | ||||||
|  | 
 | ||||||
|  |     public Address(int address) { | ||||||
|  |         this.address = address; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public int getAddress() { | ||||||
|  |         return address; | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										46
									
								
								app/src/main/java/hatelace/IntMemory.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								app/src/main/java/hatelace/IntMemory.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,46 @@ | ||||||
|  | package hatelace; | ||||||
|  | 
 | ||||||
|  | public class IntMemory extends Memory { | ||||||
|  |     protected int[] memory; | ||||||
|  | 
 | ||||||
|  |     public IntMemory() { | ||||||
|  |         this.memory = new int[256]; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public IntMemory(int size) { | ||||||
|  |         this.memory = new int[size]; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public int read(int address) { | ||||||
|  |         return this.memory[address]; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public int size() { | ||||||
|  |         return this.memory.length; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void write(int address, Word data) { | ||||||
|  |         if (address < 0 || address >= this.memory.length) { | ||||||
|  |             throw new IllegalArgumentException("Invalid memory address"); | ||||||
|  |         } | ||||||
|  |         this.memory[address] = data.getValue(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */ | ||||||
|  |     public void dump() { | ||||||
|  |         for(int i = 0; i < this.memory.length; i+= 16) { | ||||||
|  |             System.out.printf(":%02X%04X00 ", 16, i); | ||||||
|  |             for(int j = 0; j < 16; j++) { | ||||||
|  |                 int content = this.memory[i + j]; | ||||||
|  | 
 | ||||||
|  |                 // Print it as faded if it's zero | ||||||
|  |                 if (content == 0) | ||||||
|  |                     System.out.print("\u001B[2m"); | ||||||
|  | 
 | ||||||
|  |                 System.out.printf("%02X ", this.memory[i + j]); | ||||||
|  |                 System.out.print("\u001B[0m"); | ||||||
|  |             } | ||||||
|  |             System.out.println(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										29
									
								
								app/src/main/java/hatelace/IntWord.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								app/src/main/java/hatelace/IntWord.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,29 @@ | ||||||
|  | package hatelace; | ||||||
|  | 
 | ||||||
|  | public class IntWord extends Word { | ||||||
|  |     private Integer value; | ||||||
|  | 
 | ||||||
|  |     public IntWord(Integer value) { | ||||||
|  |         this.value = value; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public <T> T getValue() { | ||||||
|  |         return (T) value; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public <T> T add(Word other) { | ||||||
|  |         return (T) new IntWord(value + (Integer) other.getValue()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public <T> T subtract(Word other) { | ||||||
|  |         return (T) new IntWord(value - (Integer) other.getValue()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public <T> T multiply(Word other) { | ||||||
|  |         return (T) new IntWord(value * (Integer) other.getValue()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public <T> T divide(Word other) { | ||||||
|  |         return (T) new IntWord(value / (Integer) other.getValue()); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -2,15 +2,16 @@ package hatelace; | ||||||
| 
 | 
 | ||||||
| import hatelace.instructions.*; | import hatelace.instructions.*; | ||||||
| 
 | 
 | ||||||
| public class App { | public class Main { | ||||||
|     public static void main(String[] args) { |     public static void main(String[] args) { | ||||||
|         System.out.println("Starting..."); |         System.out.println("Starting..."); | ||||||
| 
 | 
 | ||||||
|         Memory memory = new Memory(64); // 64 words of memory |         Memory memory = new IntMemory(64); // 64 words of memory | ||||||
|         Computer computer = new Computer(memory); |         Computer computer = new Computer(memory); | ||||||
|         Program program = new Program(new Instruction[] { |         Program program = new Program(new Instruction[] { | ||||||
|             new Add(1, 1, 0), // Store 1 + 1 in address 0 |             new Add(1, 1, 0), // Store 1 + 1 in address 0 | ||||||
|             new Mul(2, 2, 1)  // Store 2 * 2 in address 1 |             new Mul(2, 2, 1),  // Store 2 * 2 in address 1 | ||||||
|  |             new Copy(new IntWord(3), new Address(2)) // Store 3 in address 2 | ||||||
|         }); |         }); | ||||||
|         computer.load(program); |         computer.load(program); | ||||||
|         computer.run(); |         computer.run(); | ||||||
|  | @ -1,46 +1,10 @@ | ||||||
| package hatelace; | package hatelace; | ||||||
| 
 | 
 | ||||||
| public class Memory { | public abstract class Memory { | ||||||
|     protected int[] memory; |     public abstract int read(int address); | ||||||
| 
 |     public abstract int size(); | ||||||
|     public Memory() { |     public abstract void write(int address, Word data); | ||||||
|         this.memory = new int[256]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public Memory(int size) { |  | ||||||
|         this.memory = new int[size]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public int read(int address) { |  | ||||||
|         return this.memory[address]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public int size() { |  | ||||||
|         return this.memory.length; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public void write(int address, int data) { |  | ||||||
|         if (address < 0 || address >= this.memory.length) { |  | ||||||
|             throw new IllegalArgumentException("Invalid memory address"); |  | ||||||
|         } |  | ||||||
|         this.memory[address] = data; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */ |     /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */ | ||||||
|     public void dump() { |     public abstract void dump(); | ||||||
|         for(int i = 0; i < this.memory.length; i+= 16) { |  | ||||||
|             System.out.printf(":%02X%04X00 ", 16, i); |  | ||||||
|             for(int j = 0; j < 16; j++) { |  | ||||||
|                 int content = this.memory[i + j]; |  | ||||||
| 
 |  | ||||||
|                 // Print it as faded if it's zero |  | ||||||
|                 if (content == 0) |  | ||||||
|                     System.out.print("\u001B[2m"); |  | ||||||
| 
 |  | ||||||
|                 System.out.printf("%02X ", this.memory[i + j]); |  | ||||||
|                 System.out.print("\u001B[0m"); |  | ||||||
|             } |  | ||||||
|             System.out.println(); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  | @ -1,5 +1,6 @@ | ||||||
| package hatelace; | package hatelace; | ||||||
| 
 | 
 | ||||||
|  | /** Simple class to represent the Program Counter */ | ||||||
| public class ProgramCounter { | public class ProgramCounter { | ||||||
|     private int PC; |     private int PC; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										9
									
								
								app/src/main/java/hatelace/Word.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								app/src/main/java/hatelace/Word.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,9 @@ | ||||||
|  | package hatelace; | ||||||
|  | 
 | ||||||
|  | public abstract class Word { | ||||||
|  |     public abstract <T> T getValue(); | ||||||
|  |     public abstract <T> T add(Word other); | ||||||
|  |     public abstract <T> T subtract(Word other); | ||||||
|  |     public abstract <T> T multiply(Word other); | ||||||
|  |     public abstract <T> T divide(Word other); | ||||||
|  | } | ||||||
|  | @ -3,6 +3,7 @@ package hatelace.instructions; | ||||||
| import hatelace.Instruction; | import hatelace.Instruction; | ||||||
| import hatelace.Memory; | import hatelace.Memory; | ||||||
| import hatelace.ProgramCounter; | import hatelace.ProgramCounter; | ||||||
|  | import hatelace.IntWord; | ||||||
| 
 | 
 | ||||||
| public class Add extends Instruction { | public class Add extends Instruction { | ||||||
|     private int op1; |     private int op1; | ||||||
|  | @ -16,7 +17,7 @@ public class Add extends Instruction { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void execute(Memory memory, ProgramCounter PC) { |     public void execute(Memory memory, ProgramCounter PC) { | ||||||
|         memory.write(this.dest, this.op1 + this.op2); |         memory.write(this.dest, new IntWord(this.op1 + this.op2)); | ||||||
|         PC.incPC(); |         PC.incPC(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										30
									
								
								app/src/main/java/hatelace/instructions/Copy.java
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								app/src/main/java/hatelace/instructions/Copy.java
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,30 @@ | ||||||
|  | package hatelace.instructions; | ||||||
|  | 
 | ||||||
|  | import hatelace.Address; | ||||||
|  | import hatelace.Word; | ||||||
|  | import hatelace.Memory; | ||||||
|  | import hatelace.ProgramCounter; | ||||||
|  | import hatelace.Instruction; | ||||||
|  | 
 | ||||||
|  | public class Copy extends Instruction { | ||||||
|  |     private Word word; | ||||||
|  |     private Address address; | ||||||
|  | 
 | ||||||
|  |     public Copy(Word word, Address address) { | ||||||
|  |         this.word = word; | ||||||
|  |         this.address = address; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void execute(Memory memory, ProgramCounter PC) { | ||||||
|  |         memory.write(this.address.getAddress(), this.word); | ||||||
|  |         PC.incPC(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected String opcode() { | ||||||
|  |         return "copy"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     protected Object[] operands() { | ||||||
|  |         return new Object[] {this.word, this.address}; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -3,6 +3,7 @@ package hatelace.instructions; | ||||||
| import hatelace.Instruction; | import hatelace.Instruction; | ||||||
| import hatelace.Memory; | import hatelace.Memory; | ||||||
| import hatelace.ProgramCounter; | import hatelace.ProgramCounter; | ||||||
|  | import hatelace.IntWord; | ||||||
| 
 | 
 | ||||||
| public class Mul extends Instruction { | public class Mul extends Instruction { | ||||||
|     private int op1; |     private int op1; | ||||||
|  | @ -16,7 +17,7 @@ public class Mul extends Instruction { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void execute(Memory memory, ProgramCounter PC) { |     public void execute(Memory memory, ProgramCounter PC) { | ||||||
|         memory.write(this.dest, this.op1 * this.op2); |         memory.write(this.dest, new IntWord(this.op1 * this.op2)); | ||||||
|         PC.incPC(); |         PC.incPC(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus