Refactor for Java 1.8
- Clean up and refactor code - Add 1.8 features - Clean up IntelliJ inspector warnings
This commit is contained in:
1 parent
a4a110dcef
commit
6e8fd40014
28 files changed
+366
-503
No files matched your search
@@ -27,6 +27,8 @@ package com.loomcom.symon.devices;
|
||||
|
||||
import com.loomcom.symon.exceptions.MemoryAccessException;
|
||||
import com.loomcom.symon.exceptions.MemoryRangeException;
|
||||
import jdk.nashorn.internal.ir.annotations.Ignore;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -37,66 +39,64 @@ import java.util.logging.Logger;
|
||||
/**
|
||||
* Emulation for the SD-card controller of the MULTICOMP system.
|
||||
* Neiter comlete nor correct.
|
||||
*
|
||||
*/
|
||||
public class SdController extends Device {
|
||||
|
||||
|
||||
private enum Status {
|
||||
IDLE,
|
||||
READ,
|
||||
WRITE
|
||||
}
|
||||
|
||||
|
||||
public static final int CONTROLLER_SIZE = 8;
|
||||
private final int SECTOR_SIZE = 512;
|
||||
private final static Logger logger = Logger.getLogger(SdController.class.getName());
|
||||
|
||||
private File sdImageFile;
|
||||
private int lba0,lba1,lba2;
|
||||
private int command;
|
||||
private int lba0, lba1, lba2;
|
||||
private int position;
|
||||
private Status status = Status.IDLE;
|
||||
|
||||
|
||||
private final byte[] readBuffer = new byte[SECTOR_SIZE];
|
||||
private final byte[] writeBuffer = new byte[SECTOR_SIZE];
|
||||
private int readPosition = 0;
|
||||
private int writePosition = 0;
|
||||
|
||||
|
||||
|
||||
|
||||
public SdController(int address) throws MemoryRangeException {
|
||||
super(address, address + CONTROLLER_SIZE - 1, "SDCONTROLLER");
|
||||
|
||||
|
||||
sdImageFile = new File("sd.img");
|
||||
if(!sdImageFile.exists()) {
|
||||
if (!sdImageFile.exists()) {
|
||||
sdImageFile = null;
|
||||
logger.log(Level.INFO, "Could not find SD card image 'sd.img'");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void write(int address, int data) throws MemoryAccessException {
|
||||
switch(address) {
|
||||
case 0 :
|
||||
switch (address) {
|
||||
case 0:
|
||||
writeData(data);
|
||||
return;
|
||||
case 1 :
|
||||
case 1:
|
||||
writeCommand(data);
|
||||
return;
|
||||
case 2 :
|
||||
case 2:
|
||||
this.lba0 = data;
|
||||
return;
|
||||
case 3 :
|
||||
case 3:
|
||||
this.lba1 = data;
|
||||
return;
|
||||
case 4 :
|
||||
case 4:
|
||||
this.lba2 = data;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(int address) throws MemoryAccessException {
|
||||
switch(address) {
|
||||
switch (address) {
|
||||
case 0:
|
||||
return readData();
|
||||
case 1:
|
||||
@@ -105,63 +105,63 @@ public class SdController extends Device {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void computePosition() {
|
||||
this.position = lba0 + (lba1 << 8) + (lba2 << 16);
|
||||
// each sector is 512 bytes, so multiply accordingly
|
||||
this.position <<= 9;
|
||||
}
|
||||
|
||||
|
||||
private void prepareRead() {
|
||||
this.status = Status.READ;
|
||||
this.readPosition = 0;
|
||||
computePosition();
|
||||
|
||||
if(sdImageFile != null) {
|
||||
|
||||
if (sdImageFile != null) {
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(sdImageFile);
|
||||
fis.skip(this.position);
|
||||
int read = fis.read(readBuffer);
|
||||
if(read < SECTOR_SIZE) {
|
||||
if (read < SECTOR_SIZE) {
|
||||
logger.log(Level.WARNING, "not enough data to fill read buffer from SD image file");
|
||||
}
|
||||
fis.close();
|
||||
} catch (IOException ex) {
|
||||
logger.log(Level.WARNING, "could not fill read buffer from SD image file", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void prepareWrite() {
|
||||
this.status = Status.WRITE;
|
||||
this.writePosition = 0;
|
||||
computePosition();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private int readData() {
|
||||
if(status != Status.READ) {
|
||||
if (status != Status.READ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int data = readBuffer[readPosition++];
|
||||
|
||||
if(readPosition >= SECTOR_SIZE) {
|
||||
|
||||
if (readPosition >= SECTOR_SIZE) {
|
||||
this.status = Status.IDLE;
|
||||
}
|
||||
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
private void writeData(int data) {
|
||||
if(status != Status.WRITE) {
|
||||
if (status != Status.WRITE) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
writeBuffer[writePosition++] = (byte) data;
|
||||
|
||||
if(writePosition >= SECTOR_SIZE) {
|
||||
if(sdImageFile != null) {
|
||||
|
||||
if (writePosition >= SECTOR_SIZE) {
|
||||
if (sdImageFile != null) {
|
||||
try {
|
||||
RandomAccessFile raf = new RandomAccessFile(sdImageFile, "rw");
|
||||
raf.skipBytes(this.position);
|
||||
@@ -171,14 +171,14 @@ public class SdController extends Device {
|
||||
logger.log(Level.WARNING, "could not write data back to SD image file!", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this.status = Status.IDLE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private int readStatus() {
|
||||
switch(this.status) {
|
||||
switch (this.status) {
|
||||
case IDLE:
|
||||
return 128;
|
||||
case READ:
|
||||
@@ -189,14 +189,13 @@ public class SdController extends Device {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void writeCommand(int data) {
|
||||
this.command = data;
|
||||
switch(this.command) {
|
||||
case 0 :
|
||||
switch (data) {
|
||||
case 0:
|
||||
prepareRead();
|
||||
return;
|
||||
case 1 :
|
||||
case 1:
|
||||
prepareWrite();
|
||||
return;
|
||||
default:
|
||||
@@ -208,5 +207,5 @@ public class SdController extends Device {
|
||||
public String toString() {
|
||||
return getName() + "@" + String.format("%04X", this.getMemoryRange().startAddress);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user