Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Jessica Hawkwell
/
kcpu
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
2
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 0cbce396
authored
Sep 29, 2017
by
Jessica Hawkwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added lots of stuffs, it executes code now
1 parent
eb9f4a4c
Pipeline
#208
passed
in 1 minute 10 seconds
Changes
20
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
669 additions
and
188 deletions
.gitignore
pom.xml
src/main/java/me/felinewith/kcpu/IMemory.java
src/main/java/me/felinewith/kcpu/KBoard.java
src/main/java/me/felinewith/kcpu/Kasm.java
src/main/java/me/felinewith/kcpu/Kcpu.java
src/main/java/me/felinewith/kcpu/Kemulator.java
src/main/java/me/felinewith/kcpu/hardware/KcmpOpCodes.java
src/main/java/me/felinewith/kcpu/hardware/Kmcp.java
src/main/java/me/felinewith/kcpu/memory/BootRom.java
src/main/java/me/felinewith/kcpu/memory/Buffer.java
src/main/java/me/felinewith/kcpu/memory/MemHelper.java
src/main/java/me/felinewith/kcpu/opcodes/BasicMath.java
src/main/java/me/felinewith/kcpu/opcodes/IHandler.java
src/main/java/me/felinewith/kcpu/opcodes/Memory.java
src/main/java/me/felinewith/kcpu/opcodes/OpDirector.java
src/main/java/me/felinewith/kcpu/opcodes/ValueHelper.java
src/main/java/me/felinewith/kcpu/util/BaseDevice.java
src/main/java/me/felinewith/kcpu/util/BaseMemory.java
test.kasm
.gitignore
View file @
0cbce39
# project-specific stuffs
*.mem
*.bin
*.kc
# swap
# swap
[._]*.s[a-v][a-z]
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]*.sw[a-p]
...
...
pom.xml
View file @
0cbce39
...
@@ -154,7 +154,7 @@
...
@@ -154,7 +154,7 @@
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>
true
</addDefaultSpecificationEntries>
<addDefaultSpecificationEntries>
true
</addDefaultSpecificationEntries>
<classpathLayoutType>
repository
</classpathLayoutType>
<classpathLayoutType>
repository
</classpathLayoutType>
<
packageName></packageName
>
<
mainClass>
me.felinewith.kcpu.Kemulator
</mainClass
>
</manifest>
</manifest>
</archive>
</archive>
</configuration>
</configuration>
...
...
src/main/java/me/felinewith/kcpu/IMemory.java
View file @
0cbce39
...
@@ -10,4 +10,6 @@ public interface IMemory {
...
@@ -10,4 +10,6 @@ public interface IMemory {
public
abstract
void
write
(
short
addr
,
byte
data
);
public
abstract
void
write
(
short
addr
,
byte
data
);
public
void
copy
(
short
addrSrc
,
short
addrDest
,
IMemory
dest
);
public
void
copy
(
short
addrSrc
,
short
addrDest
,
IMemory
dest
);
public
abstract
void
memIrq
(
short
addr
);
public
abstract
void
memIrq
(
short
addr
);
public
void
powerOff
();
}
}
src/main/java/me/felinewith/kcpu/KBoard.java
View file @
0cbce39
package
me
.
felinewith
.
kcpu
;
package
me
.
felinewith
.
kcpu
;
import
java.io.ByteArrayInputStream
;
import
java.io.File
;
import
java.io.ByteArrayOutputStream
;
import
java.io.FileOutputStream
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.io.OutputStreamWriter
;
import
java.nio.charset.Charset
;
import
java.nio.charset.Charset
;
import
java.util.Timer
;
import
java.util.Arrays
;
import
java.util.TimerTask
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
import
me.felinewith.kcpu.hardware.Kmcp
;
import
me.felinewith.kcpu.hardware.Kmcp
;
import
me.felinewith.kcpu.memory.BootRom
;
import
me.felinewith.kcpu.memory.BootRom
;
import
me.felinewith.kcpu.memory.Buffer
;
import
me.felinewith.kcpu.util.BaseDevice
;
import
me.felinewith.kcpu.util.BaseDevice
;
import
me.felinewith.kcpu.util.HardwareExec
;
import
me.felinewith.kcpu.util.HardwareExec
;
import
me.felinewith.kcpu.util.HardwareMemIrq
;
import
me.felinewith.kcpu.util.HardwareMemIrq
;
...
@@ -27,24 +22,15 @@ import me.felinewith.kcpu.util.HardwareMemIrq;
...
@@ -27,24 +22,15 @@ import me.felinewith.kcpu.util.HardwareMemIrq;
public
class
KBoard
extends
BaseDevice
implements
IMemory
{
public
class
KBoard
extends
BaseDevice
implements
IMemory
{
final
ThreadPoolExecutor
tpe
;
final
ThreadPoolExecutor
tpe
;
TimerTask
tt
;
Timer
t
;
private
final
IDevice
[]
devices
;
private
IDevice
[]
devices
;
private
final
KMemoryRange
[]
deviceMaps
;
private
KMemoryRange
[]
deviceMaps
;
private
final
IMemory
[]
memDevs
;
private
IMemory
[]
memDevs
;
private
final
KMemoryRange
[]
memDevMaps
;
private
KMemoryRange
[]
memDevMaps
;
private
final
Charset
cs
;
private
Charset
cs
;
private
Kcpu
cpu
;
private
final
InputStream
stdin
;
private
final
OutputStream
stdout
;
private
final
byte
[]
bin
;
private
final
InputStreamReader
bufin
;
private
final
OutputStreamWriter
bufout
;
public
KBoard
()
{
public
KBoard
()
{
tpe
=
new
ThreadPoolExecutor
(
16
,
32
,
30
,
TimeUnit
.
SECONDS
,
new
LinkedBlockingQueue
<>(
16
));
tpe
=
new
ThreadPoolExecutor
(
16
,
32
,
30
,
TimeUnit
.
SECONDS
,
new
LinkedBlockingQueue
<>(
16
));
...
@@ -56,26 +42,37 @@ public class KBoard extends BaseDevice implements IMemory {
...
@@ -56,26 +42,37 @@ public class KBoard extends BaseDevice implements IMemory {
memDevMaps
=
new
KMemoryRange
[
16
];
memDevMaps
=
new
KMemoryRange
[
16
];
cs
=
Charset
.
forName
(
"UTF-8"
);
cs
=
Charset
.
forName
(
"UTF-8"
);
bin
=
new
byte
[
16
];
stdin
=
new
ByteArrayInputStream
(
bin
);
stdout
=
new
ByteArrayOutputStream
(
16
);
bufin
=
new
InputStreamReader
(
stdin
);
bufout
=
new
OutputStreamWriter
(
stdout
);
}
}
public
void
init
()
{
public
void
init
()
{
attachDevice
(
0
,
0x8000
,
new
Kcpu
(
this
));
cpu
=
new
Kcpu
(
this
);
attachDevice
(
0
,
0x8000
,
cpu
);
attachDevice
(
1
,
0x8800
,
new
Kmcp
());
attachDevice
(
1
,
0x8800
,
new
Kmcp
());
attachMemory
(
0
,
0xff00
,
new
Buffer
());
//
attachMemory(0, 0xff00, new Buffer());
attachMemory
(
1
,
0x1000
,
new
BootRom
());
attachMemory
(
1
,
0x1000
,
new
BootRom
());
tt
=
new
HardwareExec
(
devices
[
0
]);
System
.
out
.
println
(
"CPU up..."
);
t
=
new
Timer
(
"CPU Cycler"
);
t
.
schedule
(
tt
,
0
,
1
);
while
(
cpu
.
workCycle
()
!=
0xff
)
{
Thread
.
yield
();
}
System
.
err
.
println
(
"CPU Stopped [zpwr]"
);
tpe
.
shutdown
();
System
.
err
.
println
(
"Timers stopped"
);
int
a
;
for
(
a
=
0
;
a
<=
0xf
;
a
++)
{
if
(
devices
[
a
]
!=
null
)
{
devices
[
a
].
powerOff
();
System
.
err
.
println
(
String
.
format
(
"device %x stopped"
,
a
));
}
if
(
memDevs
[
a
]
!=
null
)
{
memDevs
[
a
].
powerOff
();
System
.
err
.
println
(
String
.
format
(
"memdev %x stopped"
,
a
));
}
}
dumpEverything
();
}
}
@Override
public
void
memIrq
(
short
addr
)
{
@Override
public
void
memIrq
(
short
addr
)
{
...
@@ -91,9 +88,7 @@ public class KBoard extends BaseDevice implements IMemory {
...
@@ -91,9 +88,7 @@ public class KBoard extends BaseDevice implements IMemory {
short
devOff
=
memDevMaps
[
rdev
].
getOffset
();
short
devOff
=
memDevMaps
[
rdev
].
getOffset
();
short
devAddr
=
(
short
)(
addr
-
devOff
);
short
devAddr
=
(
short
)(
addr
-
devOff
);
tpe
.
execute
(
new
HardwareMemIrq
(
memDevs
[
rdev
],
devAddr
));
tpe
.
execute
(
new
HardwareMemIrq
(
memDevs
[
rdev
],
devAddr
));
memDevs
[
rdev
].
memIrq
(
devAddr
);
}
}
}
}
@Override
public
void
sendIrq
(
byte
irq
)
{
@Override
public
void
sendIrq
(
byte
irq
)
{
...
@@ -121,6 +116,7 @@ public class KBoard extends BaseDevice implements IMemory {
...
@@ -121,6 +116,7 @@ public class KBoard extends BaseDevice implements IMemory {
@Override
public
void
write
(
short
addr
,
byte
data
)
{
@Override
public
void
write
(
short
addr
,
byte
data
)
{
short
dev
=
checkDevice
(
addr
);
short
dev
=
checkDevice
(
addr
);
short
rdev
=
(
short
)(
0x0f
&
dev
);
short
rdev
=
(
short
)(
0x0f
&
dev
);
if
(
dev
==
0x7f
)
{
memory
[
0xffff
&
addr
]
=
data
;
}
if
(
dev
<=
0x0f
)
{
if
(
dev
<=
0x0f
)
{
short
devOff
=
deviceMaps
[
rdev
].
getOffset
();
short
devOff
=
deviceMaps
[
rdev
].
getOffset
();
short
devAddr
=
(
short
)(
addr
-
devOff
);
short
devAddr
=
(
short
)(
addr
-
devOff
);
...
@@ -130,15 +126,9 @@ public class KBoard extends BaseDevice implements IMemory {
...
@@ -130,15 +126,9 @@ public class KBoard extends BaseDevice implements IMemory {
short
devOff
=
memDevMaps
[
rdev
].
getOffset
();
short
devOff
=
memDevMaps
[
rdev
].
getOffset
();
short
devAddr
=
(
short
)(
addr
-
devOff
);
short
devAddr
=
(
short
)(
addr
-
devOff
);
memDevs
[
rdev
].
write
(
devAddr
,
data
);
memDevs
[
rdev
].
write
(
devAddr
,
data
);
if
(
rdev
==
0
)
{
try
{
bufout
.
write
(
0xff
&
data
);
}
catch
(
IOException
x
)
{}
}
}
}
}
memory
[
0xffff
&
addr
]
=
data
;
}
public
void
attachDevice
(
int
port
,
int
offset
,
IDevice
device
)
{
public
void
attachDevice
(
int
port
,
int
offset
,
IDevice
device
)
{
attachDevice
(
port
,
(
short
)
offset
,
device
);
}
attachDevice
(
port
,
(
short
)
offset
,
device
);
}
public
void
attachDevice
(
int
port
,
short
offset
,
IDevice
device
)
{
public
void
attachDevice
(
int
port
,
short
offset
,
IDevice
device
)
{
...
@@ -224,21 +214,76 @@ public class KBoard extends BaseDevice implements IMemory {
...
@@ -224,21 +214,76 @@ public class KBoard extends BaseDevice implements IMemory {
}
}
@Override
public
void
exec
()
{
@Override
public
void
exec
()
{
try
{
}
if
(
bufin
.
ready
()
)
{
byte
[]
temp
=
bufin
.
toString
().
getBytes
(
cs
);
int
len
=
temp
.
length
;
if
(
len
>=
16
)
{
len
=
16
;
}
public
void
dumpEverything
()
{
short
addr
;
dumpMemory
(
"system"
,
memory
);
short
a
;
int
a
;
for
(
a
=
0
;
a
<
len
;
a
++)
{
for
(
a
=
0
;
a
<
16
;
a
++)
{
addr
=
(
short
)(
0x10
+
a
);
if
(
devices
[
a
]
!=
null
)
{
dumpMemory
(
String
.
format
(
"device-%x"
,
a
),
devices
[
a
]);
}
memDevs
[
0
].
write
(
addr
,
temp
[
a
]);
}
for
(
a
=
0
;
a
<
16
;
a
++)
{
if
(
memDevs
[
a
]
!=
null
)
{
dumpMemory
(
String
.
format
(
"memDev-%x"
,
a
),
memDevs
[
a
]);
}
}
}
}
}
public
void
dumpMemory
(
String
filename
,
IMemory
mem
)
{
byte
[]
temp
=
new
byte
[
mem
.
length
()];
int
a
;
for
(
a
=
0
;
a
<
mem
.
length
();
a
++)
{
temp
[
a
]
=
mem
.
read
((
short
)
a
);
}
dumpMemory
(
filename
,
temp
);
}
}
catch
(
IOException
e
)
{}
public
void
dumpMemory
(
String
filename
,
byte
[]
mem
)
{
File
f
=
new
File
(
filename
.
concat
(
".mem"
));
try
(
FileWriter
fw
=
new
FileWriter
(
f
))
{
int
max
=
mem
.
length
-
12
;
int
a
;
for
(
a
=
0
;
a
<
max
;
a
+=
12
)
{
fw
.
write
(
String
.
format
(
"[%04x] %02x%02x : "
,
a
,
mem
[
a
],
mem
[
a
+
1
]));
fw
.
write
(
String
.
format
(
"%02x%02x -> "
,
mem
[
a
+
2
],
mem
[
a
+
3
]));
fw
.
write
(
String
.
format
(
"%02x%02x "
,
mem
[
a
+
4
],
mem
[
a
+
5
]));
fw
.
write
(
String
.
format
(
"%02x%02x "
,
mem
[
a
+
6
],
mem
[
a
+
7
]));
fw
.
write
(
String
.
format
(
"%02x%02x "
,
mem
[
a
+
8
],
mem
[
a
+
9
]));
fw
.
write
(
String
.
format
(
"%02x%02x\n"
,
mem
[
a
+
10
],
mem
[
a
+
11
]));
}
if
(
a
<
mem
.
length
)
{
while
(
a
<
mem
.
length
)
{
fw
.
write
(
String
.
format
(
"[%04x] %02x\n"
,
a
,
mem
[
a
]));
a
++;
}
}
fw
.
flush
();
fw
.
close
();
}
}
catch
(
IOException
x
)
{}
f
=
new
File
(
filename
.
concat
(
".mem.bin"
));
try
(
FileOutputStream
fos
=
new
FileOutputStream
(
f
))
{
if
(
mem
.
length
<=
4096
)
{
System
.
err
.
println
(
String
.
format
(
"%s Writing %d bytes..."
,
filename
,
mem
.
length
));
fos
.
write
(
mem
);
}
else
{
int
max
=
mem
.
length
-
4096
;
int
a
;
byte
[]
temp
;
for
(
a
=
0
;
a
<
max
;
a
+=
4096
)
{
temp
=
Arrays
.
copyOfRange
(
mem
,
a
,
a
+
4096
);
System
.
err
.
println
(
String
.
format
(
"%s Writing %d bytes..."
,
filename
,
temp
.
length
));
fos
.
write
(
temp
);
}
if
(
a
<
mem
.
length
)
{
temp
=
Arrays
.
copyOfRange
(
mem
,
a
,
mem
.
length
);
System
.
err
.
println
(
String
.
format
(
"%s Writing %d bytes..."
,
filename
,
temp
.
length
));
fos
.
write
(
temp
);
}
}
fos
.
flush
();
fos
.
close
();
}
catch
(
IOException
x
)
{}
}
}
}
src/main/java/me/felinewith/kcpu/Kasm.java
0 → 100644
View file @
0cbce39
package
me
.
felinewith
.
kcpu
;
import
java.io.BufferedReader
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.PrintStream
;
import
me.felinewith.kcpu.opcodes.OpDirector
;
/**
*
* @author jlhawkwell
*/
public
class
Kasm
{
private
static
PrintStream
outer
;
public
static
void
main
(
String
[]
args
)
{
setOuter
(
System
.
out
);
for
(
String
a
:
args
)
{
if
(
a
.
endsWith
(
".kasm"
)
)
{
try
{
assemble
(
a
);
}
catch
(
IOException
x
)
{}
}
}
}
static
void
assemble
(
String
filename
)
throws
IOException
{
assemble
(
new
File
(
filename
));}
static
void
assemble
(
File
file
)
throws
IOException
{
File
out
=
new
File
(
file
.
getCanonicalPath
().
concat
(
".kc"
));
logln
(
"\nInput : %s\nOutput: %s"
,
file
.
getCanonicalPath
(),
out
.
getCanonicalPath
());
FileInputStream
fis
=
new
FileInputStream
(
file
);
FileOutputStream
fos
=
new
FileOutputStream
(
out
);
InputStreamReader
isr
=
new
InputStreamReader
(
fis
);
BufferedReader
br
=
new
BufferedReader
(
isr
);
String
line
;
byte
[]
temp
;
int
a
;
while
((
line
=
br
.
readLine
())
!=
null
)
{
temp
=
compile
(
line
);
fos
.
write
(
temp
);
}
fos
.
flush
();
fos
.
close
();
br
.
close
();
isr
.
close
();
fis
.
close
();
}
static
public
byte
[]
compile
(
String
kasm
)
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
if
(
kasm
.
startsWith
(
"#"
)
||
kasm
.
startsWith
(
"//"
)
||
kasm
.
length
()
==
0
)
{}
else
{
String
[]
parts
;
OpDirector
opcode
;
short
ref
=
0
;
if
(
kasm
.
contains
(
" "
)
)
{
parts
=
kasm
.
split
(
" "
);
}
else
{
parts
=
new
String
[]{
kasm
};
}
parts
[
0
]
=
parts
[
0
].
toUpperCase
();
opcode
=
OpDirector
.
valueOf
(
parts
[
0
]);
log
(
"[%10s] "
,
opcode
.
name
());
writeShort
(
baos
,
opcode
.
getOpcode
());
log
(
" : "
);
if
(
parts
.
length
>=
2
)
{
ref
+=
asmType
(
parts
[
1
]);
}
if
(
parts
.
length
>=
3
)
{
ref
+=
(
short
)(
asmType
(
parts
[
2
])
<<
4
);
}
if
(
parts
.
length
>=
4
)
{
ref
+=
(
short
)(
asmType
(
parts
[
3
])
<<
8
);
}
if
(
parts
.
length
>=
5
)
{
ref
+=
(
short
)(
asmType
(
parts
[
4
])
<<
12
);
}
writeShort
(
baos
,
ref
);
log
(
" -> "
);
if
(
parts
.
length
>=
2
)
{
writeShort
(
baos
,
numberize
(
parts
[
1
]));
}
else
{
writeShort
(
baos
,
(
short
)
0
);
}
if
(
parts
.
length
>=
3
)
{
writeShort
(
baos
,
numberize
(
parts
[
2
]));
}
else
{
writeShort
(
baos
,
(
short
)
0
);
}
if
(
parts
.
length
>=
4
)
{
writeShort
(
baos
,
numberize
(
parts
[
3
]));
}
else
{
writeShort
(
baos
,
(
short
)
0
);
}
if
(
parts
.
length
>=
5
)
{
writeShort
(
baos
,
numberize
(
parts
[
4
]));
}
else
{
writeShort
(
baos
,
(
short
)
0
);
}
logln
(
""
);
}
return
baos
.
toByteArray
();
}
static
void
writeShort
(
ByteArrayOutputStream
baos
,
short
value
)
{
byte
[]
temp
=
Converter
.
short2byte
(
value
);
log
(
"%02x%02x "
,
temp
[
0
],
temp
[
1
]);
baos
.
write
(
temp
,
0
,
temp
.
length
);
}
static
void
writeShort
(
FileOutputStream
fos
,
short
value
)
throws
IOException
{
byte
[]
v
=
Converter
.
short2byte
(
value
);
log
(
"[%02x%02x]"
,
v
[
0
],
v
[
1
]);
fos
.
write
(
v
);
}
static
String
stripType
(
String
input
)
{
if
(
asmType
(
input
)
==
0
)
{
return
input
;
}
return
input
.
substring
(
1
);
}
static
short
asmType
(
String
input
)
{
return
asmType
(
input
.
charAt
(
0
));
}
static
short
asmType
(
char
input
)
{
switch
(
input
)
{
case
'r'
:
return
0x01
;
case
'%'
:
return
0x02
;
case
'$'
:
return
0x03
;
default
:
return
0x00
;
}
}
static
short
numberize
(
String
input
)
{
String
temp
=
stripType
(
input
);
String
[]
pt
;
if
(
temp
.
contains
(
"_"
)
)
{
temp
=
temp
.
replaceAll
(
"_"
,
""
);
}
if
(
temp
.
contains
(
","
)
)
{
pt
=
temp
.
split
(
"[,]"
);
short
radix
=
Short
.
valueOf
(
pt
[
1
],
10
);
return
(
short
)(
0xffff
&
Integer
.
valueOf
(
pt
[
0
],
radix
));
}
else
{
return
(
short
)(
0xffff
&
Integer
.
valueOf
(
temp
));
}
}
/**
* @return the outer
*/
public
static
PrintStream
getOuter
()
{
return
outer
;
}
/**
* @param aOuter the outer to set
*/
public
static
void
setOuter
(
PrintStream
aOuter
)
{
outer
=
aOuter
;
}
private
static
void
log
(
String
format
,
Object
...
vals
)
{
if
(
outer
==
null
)
{
return
;
}
outer
.
print
(
String
.
format
(
format
,
vals
));
}
private
static
void
logln
(
String
format
,
Object
...
vals
)
{
if
(
outer
==
null
)
{
return
;
}
outer
.
println
(
String
.
format
(
format
,
vals
));
}
}
src/main/java/me/felinewith/kcpu/Kcpu.java
View file @
0cbce39
package
me
.
felinewith
.
kcpu
;
package
me
.
felinewith
.
kcpu
;
import
java.
util.TimerTask
;
import
java.
io.PrintStream
;
import
me.felinewith.kcpu.memory.MemHelper
;
import
me.felinewith.kcpu.memory.MemHelper
;
import
me.felinewith.kcpu.opcodes.OpDirector
;
import
me.felinewith.kcpu.opcodes.OpDirector
;
import
me.felinewith.kcpu.opcodes.ValueHelper
;
import
me.felinewith.kcpu.opcodes.ValueHelper
;
import
me.felinewith.kcpu.util.BaseDevice
;
/**
/**
*
*
* @author jlhawkwell
* @author jlhawkwell
*/
*/
public
class
Kcpu
extends
TimerTask
implements
IDevice
{
public
class
Kcpu
extends
BaseDevice
{
private
static
PrintStream
outer
;
private
byte
[]
memory
;
/**
private
String
name
;
* @return the outer
*/
public
static
PrintStream
getOuter
()
{
return
outer
;
}
/**
* @param aOuter the outer to set
*/
public
static
void
setOuter
(
PrintStream
aOuter
)
{
outer
=
aOuter
;
}
private
static
void
log
(
String
format
,
Object
...
vals
)
{
if
(
outer
==
null
)
{
return
;
}
outer
.
print
(
String
.
format
(
format
,
vals
));
}
private
static
void
logln
(
String
format
,
Object
...
vals
)
{
if
(
outer
==
null
)
{
return
;
}
outer
.
println
(
String
.
format
(
format
,
vals
));
}
//private byte[] memory;
//private String name;
private
short
[]
settings
;
private
short
[]
settings
;
short
pc
;
short
pc
;
short
[]
registers
;
short
[]
registers
;
private
KBoard
board
;
private
KBoard
k
board
;
public
Kcpu
(
KBoard
system
)
{
public
Kcpu
(
KBoard
system
)
{
memory
=
new
byte
[
0x0800
];
memory
=
new
byte
[
0x0800
];
name
=
"Kcpu"
;
name
=
"Kcpu"
;
settings
=
new
short
[
6
];
settings
=
new
short
[
6
];
board
=
system
;
k
board
=
system
;
pc
=
0x1000
;
pc
=
0x1000
;
registers
=
new
short
[
4
];
registers
=
new
short
[
4
];
}
}
@Override
public
void
init
(
IMemory
system
)
{
}
@Override
public
void
init
(
IMemory
system
)
{
setOuter
(
System
.
err
);
// use this one during interrupts
OpDirector
[]
list
=
OpDirector
.
values
();
@Override
public
void
exec
()
{
for
(
OpDirector
od
:
list
)
{
logln
(
"[%10s] %04x"
,
od
.
name
(),
od
.
getOpcode
());
}
}
}
@Override
public
short
length
()
{
return
0x07ff
;
}
@Override
public
void
exec
()
{
workCycle
()
;
}
@Override
public
byte
read
(
short
addr
)
{
@Override
public
byte
read
(
short
addr
)
{
if
(
(
0xffff
&
addr
)
>
length
()
)
{
return
(
byte
)
0xff
;
}
if
(
(
0xffff
&
addr
)
>
length
()
)
{
return
(
byte
)
0xff
;
}
...
@@ -46,24 +69,19 @@ public class Kcpu extends TimerTask implements IDevice {
...
@@ -46,24 +69,19 @@ public class Kcpu extends TimerTask implements IDevice {
@Override
public
void
write
(
short
offset
,
byte
data
)
{
}
@Override
public
void
write
(
short
offset
,
byte
data
)
{
}
@Override
public
void
copy
(
short
addrSrc
,
short
addrDest
,
IMemory
dest
)
{
}
@Override
public
void
copy
(
short
addrSrc
,
short
addrDest
,
IMemory
dest
)
{
}
// this one for normal execution
@Override
public
void
run
()
{
workCycle
();
}
@Override
public
void
memIrq
(
short
addr
)
{
@Override
public
void
memIrq
(
short
addr
)
{
if
(
0
<=
addr
&&
addr
<=
memory
.
length
)
{
exec
();
}
if
(
0
<=
addr
&&
addr
<=
memory
.
length
)
{
exec
();
}
}
}
@Override
public
void
sendIrq
(
byte
irq
)
{
board
.
sendIrq
(
irq
);
}
public
int
workCycle
()
{
private
void
workCycle
()
{
if
(
pc
>=
0xfff0
)
{
pc
=
0
;
}
if
(
pc
>=
0xfff0
)
{
pc
=
0
;
}
short
inst
=
MemHelper
.
readShort
(
pc
,
board
);
short
inst
=
MemHelper
.
readShort
(
pc
,
k
board
);
short
ref
=
MemHelper
.
readShort
(
pc
+
2
,
board
);
short
ref
=
MemHelper
.
readShort
(
pc
+
2
,
k
board
);
short
[]
values
=
new
short
[
4
];
short
[]
values
=
new
short
[
4
];
values
[
0
]
=
MemHelper
.
readShort
(
pc
+
4
,
board
);
values
[
0
]
=
MemHelper
.
readShort
(
pc
+
4
,
k
board
);
values
[
1
]
=
MemHelper
.
readShort
(
pc
+
6
,
board
);
values
[
1
]
=
MemHelper
.
readShort
(
pc
+
6
,
k
board
);
values
[
2
]
=
MemHelper
.
readShort
(
pc
+
8
,
board
);
values
[
2
]
=
MemHelper
.
readShort
(
pc
+
8
,
k
board
);
values
[
3
]
=
MemHelper
.
readShort
(
pc
+
10
,
board
);
values
[
3
]
=
MemHelper
.
readShort
(
pc
+
10
,
k
board
);
OpDirector
od
=
null
;
OpDirector
od
=
null
;
for
(
OpDirector
o
:
OpDirector
.
values
())
{
for
(
OpDirector
o
:
OpDirector
.
values
())
{
...
@@ -73,43 +91,48 @@ public class Kcpu extends TimerTask implements IDevice {
...
@@ -73,43 +91,48 @@ public class Kcpu extends TimerTask implements IDevice {
}
}
}
}
if
(
od
==
null
)
{
pc
+=
12
;
return
;
}
// inc 2 if nop
logln
(
"%04x : [%10s] %04x %04x -> %04x %04x %04x %04x"
,
pc
,
((
od
==
null
)?
"nop"
:
od
.
name
()),
inst
,
ref
,
values
[
0
],
values
[
1
],
values
[
2
],
values
[
3
]
);
if
(
od
==
null
)
{
pc
+=
12
;
return
0
;
}
// inc if nop
if
(
od
.
hasHandler
()
)
{
if
(
od
.
hasHandler
()
)
{
registers
=
od
.
call
(
pc
,
ref
,
registers
,
values
,
board
);
registers
=
od
.
call
(
pc
,
ref
,
registers
,
values
,
k
board
);
pc
+=
12
;
pc
+=
12
;
return
;
return
0
;
}
}
pc
+=
12
;
pc
+=
12
;
short
data
;
short
data
;
data
=
ValueHelper
.
read
Value
(
0
,
ref
,
registers
,
values
,
board
);
data
=
ValueHelper
.
read
Short
(
0
,
ref
,
registers
,
values
,
k
board
);
switch
(
od
)
{
switch
(
od
)
{
// device management
// device management
case
DEVA:
registers
[
0
]
=
board
.
checkDevice
(
data
);
return
;
case
DEVA:
registers
[
0
]
=
kboard
.
checkDevice
(
data
);
return
0
;
case
DEVD:
board
.
detachDevice
(
data
);
return
;
case
DEVD:
kboard
.
detachDevice
(
data
);
return
0
;
case
DEVM:
case
DEVM:
short
d2
=
ValueHelper
.
read
Value
(
1
,
ref
,
registers
,
values
,
board
);
short
d2
=
ValueHelper
.
read
Short
(
1
,
ref
,
registers
,
values
,
k
board
);
board
.
setDeviceRange
(
data
,
d2
);
k
board
.
setDeviceRange
(
data
,
d2
);
return
;
return
0
;
case
DEVP:
case
DEVP:
KMemoryRange
kmr
=
board
.
getDeviceRange
(
data
);
KMemoryRange
kmr
=
k
board
.
getDeviceRange
(
data
);
registers
[
0
]
=
kmr
.
getOffset
();
registers
[
0
]
=
kmr
.
getOffset
();
registers
[
1
]
=
kmr
.
getLength
();
registers
[
1
]
=
kmr
.
getLength
();
return
;
return
0
;
// interrupts
// interrupts
case
IRQD:
board
.
sendIrq
((
byte
)(
0x000f
&
data
));
return
;
case
IRQD:
kboard
.
sendIrq
((
byte
)(
0x000f
&
data
));
return
0
;
case
IRQM:
board
.
memIrq
(
data
);
return
;
case
IRQM:
kboard
.
memIrq
(
data
);
return
0
;
// jumps
// jumps
case
JMP:
pc
=
(
short
)(
pc
+
(
0xffff
&
data
));
return
;
case
JMP:
pc
=
(
short
)(
pc
+
(
0xffff
&
data
));
return
0
;
case
JMPA:
pc
=
data
;
return
;
case
JMPA:
pc
=
data
;
return
0
;
case
JMPD:
pc
=
(
short
)(
pc
-
(
0xffff
&
data
));
return
;
case
JMPD:
pc
=
(
short
)(
pc
-
(
0xffff
&
data
));
return
0
;
case
ZPWR:
case
ZPWR:
return
0xff
;
board
.
t
.
cancel
();
}
}
return
0
;
}
}
}
}
src/main/java/me/felinewith/kcpu/Kemulator.java
0 → 100644
View file @
0cbce39
package
me
.
felinewith
.
kcpu
;
import
me.felinewith.kcpu.memory.Buffer
;
/**
*
* @author jlhawkwell
*/
public
class
Kemulator
{
public
static
void
main
(
String
[]
args
)
{
KBoard
kb
=
new
KBoard
();
Buffer
bb
=
new
Buffer
();
for
(
String
s
:
args
)
{
System
.
out
.
println
(
String
.
format
(
"\t%s"
,
s
));
}
if
(
args
.
length
!=
0
)
{
for
(
String
s
:
args
)
{
if
(
s
.
equals
(
"-kasm"
))
{
Kasm
.
main
(
args
);
return
;
}
}
}
bb
.
setInputStream
(
System
.
in
);
bb
.
setOutputStream
(
System
.
out
);
kb
.
attachMemory
(
0
,
0xff00
,
bb
);
kb
.
init
();
System
.
out
.
flush
();
}
}
src/main/java/me/felinewith/kcpu/hardware/KcmpOpCodes.java
0 → 100644
View file @
0cbce39
package
me
.
felinewith
.
kcpu
.
hardware
;
/**
*
* @author jlhawkwell
*/
public
enum
KcmpOpCodes
{
// basic math stuffies
ADD
(
0x0001
),
SUB
(
0x0002
),
MUL
(
0x0003
),
DIV
(
0x0004
),
// everything here is same as above
;
short
o
;
private
KcmpOpCodes
(
short
opcode
)
{
o
=
opcode
;
}
private
KcmpOpCodes
(
int
opcode
)
{
o
=
(
short
)(
0xffff
&
opcode
);
}
public
short
getOpcode
()
{
return
o
;
}
}
src/main/java/me/felinewith/kcpu/hardware/Kmcp.java
View file @
0cbce39
package
me
.
felinewith
.
kcpu
.
hardware
;
package
me
.
felinewith
.
kcpu
.
hardware
;
import
java.io.PrintStream
;
import
java.util.Arrays
;
import
me.felinewith.kcpu.Converter
;
import
me.felinewith.kcpu.Converter
;
import
me.felinewith.kcpu.util.BaseDevice
;
import
me.felinewith.kcpu.util.BaseDevice
;
...
@@ -8,8 +10,29 @@ import me.felinewith.kcpu.util.BaseDevice;
...
@@ -8,8 +10,29 @@ import me.felinewith.kcpu.util.BaseDevice;
* @author jlhawkwell
* @author jlhawkwell
*/
*/
public
class
Kmcp
extends
BaseDevice
{
public
class
Kmcp
extends
BaseDevice
{
private
static
PrintStream
outer
;
/**
* @return the outer
*/
public
static
PrintStream
getOuter
()
{
return
outer
;
}
/**
* @param aOuter the outer to set
*/
public
static
void
setOuter
(
PrintStream
aOuter
)
{
outer
=
aOuter
;
}
private
static
void
log
(
String
format
,
Object
...
vals
)
{
if
(
outer
==
null
)
{
return
;
}
outer
.
print
(
String
.
format
(
format
,
vals
));
}
private
static
void
logln
(
String
format
,
Object
...
vals
)
{
if
(
outer
==
null
)
{
return
;
}
outer
.
println
(
String
.
format
(
format
,
vals
));
}
public
Kmcp
()
{
public
Kmcp
()
{
setOuter
(
System
.
err
);
memory
=
new
byte
[
0x20
];
memory
=
new
byte
[
0x20
];
name
=
"Kmcp"
;
name
=
"Kmcp"
;
}
}
...
@@ -39,43 +62,46 @@ public class Kmcp extends BaseDevice {
...
@@ -39,43 +62,46 @@ public class Kmcp extends BaseDevice {
read
(
starts
+
1
)
read
(
starts
+
1
)
};
};
short
opcode
=
Converter
.
byte2short
(
data
);
short
opcode
=
Converter
.
byte2short
(
data
);
a
=
(
short
)(
0xff
&
opcode
);
short
tmpcode
=
(
short
)(
0x7fff
&
opcode
);
temp
=
do2reg
(
a
,
reg
[
0
],
reg
[
1
]);
KcmpOpCodes
od
=
null
;
reg
[
0
]
=
temp
[
0
];
for
(
KcmpOpCodes
o
:
KcmpOpCodes
.
values
())
{
reg
[
1
]
=
temp
[
1
];
if
(
o
.
getOpcode
()
==
tmpcode
)
{
od
=
o
;
break
;
}
}
logln
(
"Kmcp : [%10s] %04x -> %04x %04x %04x %04x"
,
((
od
==
null
)?
"nop"
:
od
.
name
()),
opcode
,
reg
[
0
],
reg
[
1
],
reg
[
2
],
reg
[
3
]
);
a
=
(
short
)((
0xff00
&
opcode
)
>>
8
);
reg
=
performCalc
(
od
,
reg
);
temp
=
do2reg
(
a
,
reg
[
2
],
reg
[
3
]);
reg
[
2
]
=
temp
[
0
];
reg
[
3
]
=
temp
[
1
];
for
(
a
=
0
;
a
<=
3
;
a
++
)
{
for
(
a
=
0
;
a
<=
3
;
a
++
)
{
writeReg
(
setting
,
a
,
reg
[
a
]);
writeReg
(
setting
,
a
,
reg
[
a
]);
}
}
}
}
private
short
[]
do2reg
(
short
opcode
,
short
reg0
,
short
reg1
)
{
private
short
[]
performCalc
(
KcmpOpCodes
opcode
,
short
[]
reg
)
{
short
[]
out
=
new
short
[
2
];
short
[]
out
=
Arrays
.
copyOf
(
reg
,
reg
.
length
);
out
[
0
]
=
reg0
;
out
[
1
]
=
reg1
;
switch
(
opcode
)
{
switch
(
opcode
)
{
case
0x10
:
// add
case
ADD
:
// add
out
[
0
]
=
(
short
)(
reg
0
+
reg1
);
out
[
0
]
=
(
short
)(
reg
[
0
]
+
reg
[
1
]
);
break
;
break
;
case
0x11
:
// subt
case
DIV:
// div
out
[
0
]
=
(
short
)(
reg0
-
reg1
);
out
[
0
]
=
(
short
)(
reg
[
0
]
/
reg
[
1
]);
out
[
1
]
=
(
short
)(
reg
[
0
]
%
reg
[
1
]);
break
;
break
;
case
0x12
:
// mul
t
case
SUB:
// sub
t
out
[
0
]
=
(
short
)(
reg
0
*
reg1
);
out
[
0
]
=
(
short
)(
reg
[
0
]
-
reg
[
1
]
);
break
;
break
;
case
0x13
:
// div
case
MUL:
// mult
out
[
0
]
=
(
short
)(
reg0
/
reg1
);
out
[
0
]
=
(
short
)(
reg
[
0
]
*
reg
[
1
]);
out
[
1
]
=
(
short
)(
reg0
%
reg1
);
break
;
break
;
}
}
return
new
short
[]{
reg0
,
reg1
}
;
return
out
;
}
}
private
short
readReg
(
short
setting
,
short
reg
)
{
private
short
readReg
(
short
setting
,
short
reg
)
{
...
...
src/main/java/me/felinewith/kcpu/memory/BootRom.java
View file @
0cbce39
package
me
.
felinewith
.
kcpu
.
memory
;
package
me
.
felinewith
.
kcpu
.
memory
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
me.felinewith.kcpu.Converter
;
import
me.felinewith.kcpu.Kasm
;
import
me.felinewith.kcpu.util.BaseMemory
;
import
me.felinewith.kcpu.util.BaseMemory
;
/**
/**
...
@@ -9,18 +11,52 @@ import me.felinewith.kcpu.util.BaseMemory;
...
@@ -9,18 +11,52 @@ import me.felinewith.kcpu.util.BaseMemory;
*/
*/
public
class
BootRom
extends
BaseMemory
{
public
class
BootRom
extends
BaseMemory
{
private
short
writepos
;
public
BootRom
()
{
public
BootRom
()
{
writepos
=
0
;
memory
=
new
byte
[
0x1000
];
memory
=
new
byte
[
0x1000
];
Arrays
.
fill
(
memory
,
(
byte
)
0xff
);
Arrays
.
fill
(
memory
,
(
byte
)
0xff
);
// hardcoding some bootrom
// hardcoding some bootrom
memory
[
0
]
=
0x11
;
// clearing the registers is as close to a reset as we can get
memory
[
1
]
=
0x00
;
writeBytes
(
Kasm
.
compile
(
"movs r0 0"
));
memory
[
2
]
=
0x01
;
writeBytes
(
Kasm
.
compile
(
"movs r1 0"
));
memory
[
3
]
=
0x00
;
writeBytes
(
Kasm
.
compile
(
"movs r2 0"
));
memory
[
4
]
=
0x61
;
writeBytes
(
Kasm
.
compile
(
"movs r3 0"
));
memory
[
5
]
=
0x41
;
writeBytes
(
Kasm
.
compile
(
"mov ff00,16 61,16"
));
writeBytes
(
Kasm
.
compile
(
"irqm ff01,16"
));
}
}
@Override
public
void
write
(
short
offset
,
byte
data
)
{
}
@Override
public
void
write
(
short
offset
,
byte
data
)
{
}
private
void
writeInt
(
int
input
)
{
byte
[]
data
=
Converter
.
int2byte
(
input
);
writeByte
(
data
[
3
]);
writeByte
(
data
[
2
]);
writeByte
(
data
[
1
]);
writeByte
(
data
[
0
]);
}
private
void
writeInt
(
short
input
)
{
writeInt
(
0xffff
&
input
);
}
private
void
writeInt
(
byte
input
)
{
writeInt
(
0xff
&
input
);
}
private
void
writeShort
(
int
input
)
{
writeShort
((
short
)(
0xffff
&
input
));
}
private
void
writeShort
(
short
input
)
{
byte
[]
data
=
Converter
.
short2byte
(
input
);
writeByte
(
data
[
1
]);
writeByte
(
data
[
0
]);
}
private
void
writeShort
(
byte
input
)
{
writeShort
((
short
)(
0xff
&
input
));
}
private
void
writeByte
(
int
input
)
{
writeByte
((
byte
)(
0xff
&
input
));
}
private
void
writeByte
(
short
input
)
{
writeByte
((
byte
)(
0xff
&
input
));
}
private
void
writeByte
(
byte
input
)
{
memory
[
writepos
]
=
input
;
writepos
++;
if
(
writepos
==
memory
.
length
)
{
writepos
=
0
;
}
}
private
void
writeBytes
(
byte
[]
input
)
{
for
(
byte
b
:
input
)
{
writeByte
(
b
);
}
}
}
}
src/main/java/me/felinewith/kcpu/memory/Buffer.java
View file @
0cbce39
package
me
.
felinewith
.
kcpu
.
memory
;
package
me
.
felinewith
.
kcpu
.
memory
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.io.OutputStreamWriter
;
import
java.nio.charset.Charset
;
import
me.felinewith.kcpu.util.BaseMemory
;
import
me.felinewith.kcpu.util.BaseMemory
;
/**
/**
...
@@ -8,7 +16,87 @@ import me.felinewith.kcpu.util.BaseMemory;
...
@@ -8,7 +16,87 @@ import me.felinewith.kcpu.util.BaseMemory;
*/
*/
public
class
Buffer
extends
BaseMemory
{
public
class
Buffer
extends
BaseMemory
{
private
InputStream
stdin
;
private
OutputStream
stdout
;
private
byte
[]
bin
;
private
Charset
cs
;
private
InputStreamReader
bufin
;
private
OutputStreamWriter
bufout
;
public
Buffer
()
{
public
Buffer
()
{
memory
=
new
byte
[
32
];
memory
=
new
byte
[
32
];
bin
=
new
byte
[
16
];
stdin
=
new
ByteArrayInputStream
(
bin
);
stdout
=
new
ByteArrayOutputStream
(
16
);
cs
=
Charset
.
forName
(
"UTF-8"
);
bufin
=
new
InputStreamReader
(
stdin
);
bufout
=
new
OutputStreamWriter
(
stdout
);
}
@Override
public
void
memIrq
(
short
addr
)
{
short
a
;
short
value
;
if
(
addr
<=
0x0f
)
{
try
{
for
(
a
=
0
;
a
<=
0x0f
;
a
++)
{
if
(
memory
[
a
]
==
0
)
{
break
;
}
bufout
.
write
(
memory
[
a
]);
memory
[
a
]
=
0
;
}
bufout
.
flush
();
}
catch
(
IOException
x
)
{}
}
if
(
addr
>=
0x10
)
{
try
{
if
(
bufin
.
ready
()
)
{
byte
[]
temp
=
bufin
.
toString
().
getBytes
(
cs
);
int
len
=
temp
.
length
;
if
(
len
>=
16
)
{
len
=
16
;
}
for
(
a
=
0
;
a
<
len
;
a
++)
{
value
=
(
short
)(
0x10
+
a
);
memory
[
value
]
=
temp
[
a
];
}
}
}
catch
(
IOException
e
)
{}
}
}
@Override
public
byte
read
(
short
addr
)
{
if
(
addr
>=
memory
.
length
)
{
return
(
byte
)
0xff
;
}
// input buffer clears when read
if
(
addr
>=
0x10
)
{
byte
b
=
memory
[
addr
];
memory
[
addr
]
=
0
;
return
b
;
}
return
memory
[
addr
];
}
@Override
public
void
write
(
short
addr
,
byte
data
)
{
// doing this to prevent programs from clobbering the input buffer
if
(
addr
>=
0x10
)
{
return
;
}
super
.
write
(
addr
,
data
);
}
public
InputStream
getInputStream
()
{
return
stdin
;
}
public
OutputStream
getOutputStream
()
{
return
stdout
;
}
public
void
setInputStream
(
InputStream
is
)
{
stdin
=
is
;
bufin
=
new
InputStreamReader
(
stdin
);
}
public
void
setOutputStream
(
OutputStream
os
)
{
stdout
=
os
;
bufout
=
new
OutputStreamWriter
(
stdout
);
}
}
}
}
src/main/java/me/felinewith/kcpu/memory/MemHelper.java
View file @
0cbce39
...
@@ -8,6 +8,18 @@ import me.felinewith.kcpu.IMemory;
...
@@ -8,6 +8,18 @@ import me.felinewith.kcpu.IMemory;
* @author jlhawkwell
* @author jlhawkwell
*/
*/
public
class
MemHelper
{
public
class
MemHelper
{
public
static
byte
readByte
(
int
addr
,
IMemory
memory
)
{
return
readByte
((
short
)
addr
,
memory
);
}
public
static
byte
readByte
(
short
addr
,
IMemory
memory
)
{
return
memory
.
read
(
addr
);
}
public
static
void
writeByte
(
int
addr
,
int
data
,
IMemory
memory
)
{
writeByte
((
short
)
addr
,
(
byte
)
data
,
memory
);
}
public
static
void
writeByte
(
int
addr
,
short
data
,
IMemory
memory
)
{
writeByte
((
short
)
addr
,
(
byte
)
data
,
memory
);
}
public
static
void
writeByte
(
int
addr
,
byte
data
,
IMemory
memory
)
{
writeByte
((
short
)
addr
,
data
,
memory
);
}
public
static
void
writeByte
(
short
addr
,
int
data
,
IMemory
memory
)
{
writeByte
(
addr
,
(
byte
)
data
,
memory
);
}
public
static
void
writeByte
(
short
addr
,
short
data
,
IMemory
memory
)
{
writeByte
(
addr
,
(
byte
)
data
,
memory
);
}
public
static
void
writeByte
(
short
addr
,
byte
data
,
IMemory
memory
)
{
memory
.
write
(
addr
,
data
);
}
public
static
short
readShort
(
int
addr
,
IMemory
memory
)
{
return
readShort
((
short
)
addr
,
memory
);
}
public
static
short
readShort
(
int
addr
,
IMemory
memory
)
{
return
readShort
((
short
)
addr
,
memory
);
}
public
static
short
readShort
(
short
addr
,
IMemory
memory
)
{
public
static
short
readShort
(
short
addr
,
IMemory
memory
)
{
byte
[]
data
=
new
byte
[
2
];
byte
[]
data
=
new
byte
[
2
];
...
@@ -16,12 +28,14 @@ public class MemHelper {
...
@@ -16,12 +28,14 @@ public class MemHelper {
return
Converter
.
byte2short
(
data
);
return
Converter
.
byte2short
(
data
);
}
}
public
static
void
writeShort
(
int
addr
,
int
data
,
IMemory
memory
)
{
writeShort
((
short
)
addr
,
(
short
)
data
,
memory
);
}
public
static
void
writeShort
(
int
addr
,
short
data
,
IMemory
memory
)
{
writeShort
((
short
)
addr
,
data
,
memory
);
}
public
static
void
writeShort
(
int
addr
,
short
data
,
IMemory
memory
)
{
writeShort
((
short
)
addr
,
data
,
memory
);
}
public
static
void
writeShort
(
int
addr
,
byte
data
,
IMemory
memory
)
{
writeShort
((
short
)
addr
,
(
short
)
data
,
memory
);
}
public
static
void
writeShort
(
short
addr
,
int
data
,
IMemory
memory
)
{
writeShort
(
addr
,
(
short
)
data
,
memory
);
}
public
static
void
writeShort
(
short
addr
,
int
data
,
IMemory
memory
)
{
writeShort
(
addr
,
(
short
)
data
,
memory
);
}
public
static
void
writeShort
(
int
addr
,
int
data
,
IMemory
memory
)
{
writeShort
((
short
)
addr
,
(
short
)
data
,
memory
);
}
public
static
void
writeShort
(
short
addr
,
short
data
,
IMemory
memory
)
{
public
static
void
writeShort
(
short
addr
,
short
data
,
IMemory
memory
)
{
byte
[]
dt
=
Converter
.
short2byte
(
data
);
byte
[]
dt
=
Converter
.
short2byte
(
data
);
memory
.
write
(
addr
,
dt
[
0
]);
memory
.
write
(
addr
,
dt
[
0
]);
memory
.
write
((
short
)(
addr
+
1
),
dt
[
1
]);
memory
.
write
((
short
)(
addr
+
1
),
dt
[
1
]);
}
}
public
static
void
writeShort
(
short
addr
,
byte
data
,
IMemory
memory
)
{
writeShort
(
addr
,
(
short
)
data
,
memory
);
}
}
}
src/main/java/me/felinewith/kcpu/opcodes/BasicMath.java
View file @
0cbce39
...
@@ -11,8 +11,8 @@ public class BasicMath implements IHandler {
...
@@ -11,8 +11,8 @@ public class BasicMath implements IHandler {
@Override
public
short
[]
opcode
(
OpDirector
opcode
,
short
ref
,
short
pc
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
@Override
public
short
[]
opcode
(
OpDirector
opcode
,
short
ref
,
short
pc
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
short
[]
output
=
Arrays
.
copyOf
(
regs
,
regs
.
length
);
short
[]
output
=
Arrays
.
copyOf
(
regs
,
regs
.
length
);
short
va
=
ValueHelper
.
read
Value
(
0
,
ref
,
output
,
values
,
memory
);
short
va
=
ValueHelper
.
read
Short
(
0
,
ref
,
output
,
values
,
memory
);
short
vb
=
ValueHelper
.
read
Value
(
1
,
ref
,
output
,
values
,
memory
);
short
vb
=
ValueHelper
.
read
Short
(
1
,
ref
,
output
,
values
,
memory
);
switch
(
opcode
)
{
switch
(
opcode
)
{
case
ADD:
output
[
0
]
=
(
short
)(
va
+
vb
);
break
;
case
ADD:
output
[
0
]
=
(
short
)(
va
+
vb
);
break
;
...
@@ -25,8 +25,4 @@ public class BasicMath implements IHandler {
...
@@ -25,8 +25,4 @@ public class BasicMath implements IHandler {
}
}
return
output
;
return
output
;
}
}
@Override
public
int
getArgs
(
OpDirector
opcode
)
{
return
2
;
}
}
}
src/main/java/me/felinewith/kcpu/opcodes/IHandler.java
View file @
0cbce39
...
@@ -8,5 +8,4 @@ import me.felinewith.kcpu.IMemory;
...
@@ -8,5 +8,4 @@ import me.felinewith.kcpu.IMemory;
*/
*/
public
interface
IHandler
{
public
interface
IHandler
{
public
abstract
short
[]
opcode
(
OpDirector
opcode
,
short
ref
,
short
pc
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
);
public
abstract
short
[]
opcode
(
OpDirector
opcode
,
short
ref
,
short
pc
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
);
public
abstract
int
getArgs
(
OpDirector
opcode
);
}
}
src/main/java/me/felinewith/kcpu/opcodes/Memory.java
View file @
0cbce39
...
@@ -2,7 +2,6 @@ package me.felinewith.kcpu.opcodes;
...
@@ -2,7 +2,6 @@ package me.felinewith.kcpu.opcodes;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
me.felinewith.kcpu.IMemory
;
import
me.felinewith.kcpu.IMemory
;
import
me.felinewith.kcpu.memory.MemHelper
;
/**
/**
*
*
...
@@ -10,44 +9,20 @@ import me.felinewith.kcpu.memory.MemHelper;
...
@@ -10,44 +9,20 @@ import me.felinewith.kcpu.memory.MemHelper;
*/
*/
public
class
Memory
implements
IHandler
{
public
class
Memory
implements
IHandler
{
@Override
public
short
[]
opcode
(
OpDirector
opcode
,
short
ref
,
short
pc
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
@Override
public
short
[]
opcode
(
OpDirector
opcode
,
short
ref
,
short
pc
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
short
a
=
MemHelper
.
readShort
(
pc
+
2
,
memory
);
short
b
=
MemHelper
.
readShort
(
pc
+
4
,
memory
);
short
data
;
short
data
;
short
[]
output
=
Arrays
.
copyOf
(
regs
,
regs
.
length
);
short
[]
output
=
Arrays
.
copyOf
(
regs
,
regs
.
length
);
switch
(
opcode
)
{
switch
(
opcode
)
{
case
MOV:
// movrr
case
MOV:
case
MOVB:
data
=
ValueHelper
.
read
Value
(
0
,
ref
,
output
,
values
,
memory
);
data
=
ValueHelper
.
read
Byte
(
1
,
ref
,
output
,
values
,
memory
);
output
=
ValueHelper
.
write
Value
(
1
,
ref
,
output
,
values
,
memory
,
data
);
output
=
ValueHelper
.
write
Byte
(
0
,
ref
,
output
,
values
,
memory
,
data
);
return
output
;
return
output
;
// case MOVRD: // movrd
case
MOVS:
// output[ra] = b;
data
=
ValueHelper
.
readShort
(
1
,
ref
,
output
,
values
,
memory
);
// return output;
output
=
ValueHelper
.
writeShort
(
0
,
ref
,
output
,
values
,
memory
,
data
);
// case MOVRA: // movra
// output[ra] = MemHelper.readShort(b, memory);
// return output;
// case MOVDR: // movdr
// MemHelper.writeShort(a, output[rb], memory);
// return output;
// case MOVAR: // movar
// MemHelper.writeShort(MemHelper.readShort(a, memory), output[rb], memory);
// return output;
// case MOVAD: // movad
// MemHelper.writeShort(MemHelper.readShort(a, memory), b, memory);
// return output;
// case MOVAA: // movaa
// MemHelper.writeShort(MemHelper.readShort(a, memory), MemHelper.readShort(b, memory), memory);
// return output;
}
return
output
;
return
output
;
}
}
@Override
public
int
getArgs
(
OpDirector
opcode
)
{
return
output
;
switch
(
opcode
)
{
case
MOV:
// movrr
return
2
;
}
return
0
;
}
}
}
}
src/main/java/me/felinewith/kcpu/opcodes/OpDirector.java
View file @
0cbce39
...
@@ -15,7 +15,9 @@ public enum OpDirector {
...
@@ -15,7 +15,9 @@ public enum OpDirector {
DIV
(
0x0004
,
BasicMath
.
class
),
DIV
(
0x0004
,
BasicMath
.
class
),
// memory stuffies
// memory stuffies
MOV
(
0x0011
,
Memory
.
class
),
MOV
(
0x0010
,
Memory
.
class
),
MOVB
(
0x0010
,
Memory
.
class
),
MOVS
(
0x0011
,
Memory
.
class
),
// cpu stuffies
// cpu stuffies
DEVA
(
0xff00
,
null
),
// get device port from address
DEVA
(
0xff00
,
null
),
// get device port from address
...
...
src/main/java/me/felinewith/kcpu/opcodes/ValueHelper.java
View file @
0cbce39
...
@@ -9,36 +9,98 @@ import me.felinewith.kcpu.memory.MemHelper;
...
@@ -9,36 +9,98 @@ import me.felinewith.kcpu.memory.MemHelper;
* @author jmahoney5
* @author jmahoney5
*/
*/
public
class
ValueHelper
{
public
class
ValueHelper
{
public
static
short
read
Value
(
int
pos
,
int
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
public
static
short
read
Byte
(
int
pos
,
int
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
return
read
Value
((
short
)
pos
,
(
short
)
ref
,
regs
,
values
,
memory
);
return
read
Byte
((
short
)
pos
,
(
short
)
ref
,
regs
,
values
,
memory
);
}
}
//public static short readValue(int ref, short dval, int register, short[] regs, IMemory memory) {
public
static
short
readByte
(
short
pos
,
short
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
// return readValue((short)ref, dval, (short)register, regs, memory);
//}
public
static
short
readValue
(
short
pos
,
short
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
short
output
=
0
;
short
output
=
0
;
short
reg
=
(
short
)(
0x03
&
values
[
pos
]);
short
reg
=
(
short
)(
0x03
&
values
[
pos
]);
// 01: direct
// 00: direct
// 02: register
// 01: register
// 03: direct address
// 02: direct address
// 04: register address
// 03: register address
switch
(
ref
)
{
short
cref
=
0
;
switch
(
pos
)
{
case
0
:
cref
=
(
short
)(
0x000f
&
ref
);
break
;
case
1
:
cref
=
(
short
)(
0x00f0
&
ref
);
break
;
case
2
:
cref
=
(
short
)(
0x0f00
&
ref
);
break
;
case
3
:
cref
=
(
short
)(
0xf000
&
ref
);
break
;
}
switch
(
cref
)
{
case
0x0000
:
output
=
values
[
pos
];
break
;
case
0x0001
:
case
0x0010
:
case
0x0100
:
case
0x1000
:
case
0x0001
:
case
0x0010
:
case
0x0100
:
case
0x1000
:
output
=
values
[
pos
];
break
;
case
0x0002
:
case
0x0020
:
case
0x0200
:
case
0x2000
:
output
=
regs
[
reg
];
break
;
output
=
regs
[
reg
];
break
;
case
0x0002
:
case
0x0020
:
case
0x0200
:
case
0x2000
:
output
=
MemHelper
.
readByte
(
values
[
pos
],
memory
);
break
;
case
0x0003
:
case
0x0030
:
case
0x0300
:
case
0x3000
:
case
0x0003
:
case
0x0030
:
case
0x0300
:
case
0x3000
:
output
=
MemHelper
.
readByte
(
regs
[
reg
],
memory
);
break
;
}
return
output
;
}
public
static
short
[]
writeByte
(
int
pos
,
int
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
,
short
data
)
{
return
writeByte
((
short
)
pos
,
(
short
)
ref
,
regs
,
values
,
memory
,
(
byte
)(
0xff
&
data
));
}
public
static
short
[]
writeByte
(
short
pos
,
short
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
,
short
data
)
{
return
writeByte
(
pos
,
ref
,
regs
,
values
,
memory
,
(
byte
)(
0xff
&
data
));
}
public
static
short
[]
writeByte
(
int
pos
,
int
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
,
byte
data
)
{
return
writeByte
((
short
)
pos
,
(
short
)
ref
,
regs
,
values
,
memory
,
data
);
}
public
static
short
[]
writeByte
(
short
pos
,
short
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
,
byte
data
)
{
short
[]
output
=
Arrays
.
copyOf
(
regs
,
regs
.
length
);
short
reg
=
(
short
)(
0x03
&
values
[
pos
]);
short
cref
=
0
;
switch
(
pos
)
{
case
0
:
cref
=
(
short
)(
0x000f
&
ref
);
break
;
case
1
:
cref
=
(
short
)(
0x00f0
&
ref
);
break
;
case
2
:
cref
=
(
short
)(
0x0f00
&
ref
);
break
;
case
3
:
cref
=
(
short
)(
0xf000
&
ref
);
break
;
}
switch
(
cref
)
{
case
0x0001
:
case
0x0010
:
case
0x0100
:
case
0x1000
:
output
[
reg
]
=
data
;
break
;
case
0x0000
:
case
0x0002
:
case
0x0020
:
case
0x0200
:
case
0x2000
:
MemHelper
.
writeByte
(
values
[
pos
],
data
,
memory
);
break
;
case
0x0003
:
case
0x0030
:
case
0x0300
:
case
0x3000
:
MemHelper
.
writeByte
(
output
[
reg
],
data
,
memory
);
break
;
}
return
output
;
}
public
static
short
readShort
(
int
pos
,
int
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
return
readShort
((
short
)
pos
,
(
short
)
ref
,
regs
,
values
,
memory
);
}
public
static
short
readShort
(
short
pos
,
short
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
)
{
short
output
=
0
;
short
reg
=
(
short
)(
0x03
&
values
[
pos
]);
// 00: direct
// 01: register
// 02: direct address
// 03: register address
short
cref
=
0
;
switch
(
pos
)
{
case
0
:
cref
=
(
short
)(
0x000f
&
ref
);
break
;
case
1
:
cref
=
(
short
)(
0x00f0
&
ref
);
break
;
case
2
:
cref
=
(
short
)(
0x0f00
&
ref
);
break
;
case
3
:
cref
=
(
short
)(
0xf000
&
ref
);
break
;
}
switch
(
cref
)
{
case
0x0000
:
output
=
values
[
pos
];
break
;
case
0x0001
:
case
0x0010
:
case
0x0100
:
case
0x1000
:
output
=
regs
[
reg
];
break
;
case
0x0002
:
case
0x0020
:
case
0x0200
:
case
0x2000
:
output
=
MemHelper
.
readShort
(
values
[
pos
],
memory
);
break
;
output
=
MemHelper
.
readShort
(
values
[
pos
],
memory
);
break
;
case
0x000
4
:
case
0x0040
:
case
0x0400
:
case
0x4000
:
case
0x000
3
:
case
0x0030
:
case
0x0300
:
case
0x3000
:
output
=
MemHelper
.
readShort
(
regs
[
reg
],
memory
);
break
;
output
=
MemHelper
.
readShort
(
regs
[
reg
],
memory
);
break
;
}
}
return
output
;
return
output
;
}
}
public
static
short
[]
write
Value
(
int
pos
,
int
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
,
short
data
)
{
public
static
short
[]
write
Short
(
int
pos
,
int
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
,
short
data
)
{
return
write
Value
((
short
)
pos
,
(
short
)
ref
,
regs
,
values
,
memory
,
data
);
return
write
Short
((
short
)
pos
,
(
short
)
ref
,
regs
,
values
,
memory
,
data
);
}
}
public
static
short
[]
write
Value
(
short
pos
,
short
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
,
short
data
)
{
public
static
short
[]
write
Short
(
short
pos
,
short
ref
,
short
[]
regs
,
short
[]
values
,
IMemory
memory
,
short
data
)
{
short
[]
output
=
Arrays
.
copyOf
(
regs
,
regs
.
length
);
short
[]
output
=
Arrays
.
copyOf
(
regs
,
regs
.
length
);
short
reg
=
(
short
)(
0x03
&
values
[
pos
]);
short
reg
=
(
short
)(
0x03
&
values
[
pos
]);
short
cref
=
0
;
short
cref
=
0
;
...
@@ -49,12 +111,12 @@ public class ValueHelper {
...
@@ -49,12 +111,12 @@ public class ValueHelper {
case
3
:
cref
=
(
short
)(
0xf000
&
ref
);
break
;
case
3
:
cref
=
(
short
)(
0xf000
&
ref
);
break
;
}
}
switch
(
cref
)
{
switch
(
cref
)
{
case
0x0002
:
case
0x0020
:
case
0x0200
:
case
0x2000
:
output
[
reg
]
=
data
;
break
;
case
0x0001
:
case
0x0010
:
case
0x0100
:
case
0x1000
:
case
0x0001
:
case
0x0010
:
case
0x0100
:
case
0x1000
:
case
0x0003
:
case
0x0030
:
case
0x0300
:
case
0x3000
:
output
[
reg
]
=
data
;
break
;
case
0x0000
:
case
0x0002
:
case
0x0020
:
case
0x0200
:
case
0x2000
:
MemHelper
.
writeShort
(
values
[
pos
],
data
,
memory
);
break
;
MemHelper
.
writeShort
(
values
[
pos
],
data
,
memory
);
break
;
case
0x000
4
:
case
0x0040
:
case
0x0400
:
case
0x4000
:
case
0x000
3
:
case
0x0030
:
case
0x0300
:
case
0x3000
:
MemHelper
.
writeShort
(
output
[
reg
],
data
,
memory
);
break
;
MemHelper
.
writeShort
(
output
[
reg
],
data
,
memory
);
break
;
}
}
return
output
;
return
output
;
...
...
src/main/java/me/felinewith/kcpu/util/BaseDevice.java
View file @
0cbce39
...
@@ -25,6 +25,8 @@ public abstract class BaseDevice extends BaseMemory implements IDevice {
...
@@ -25,6 +25,8 @@ public abstract class BaseDevice extends BaseMemory implements IDevice {
sendIrq
(
memory
[
addr
]);
sendIrq
(
memory
[
addr
]);
}
}
@Override
public
void
sendIrq
(
byte
irq
)
{
}
@Override
public
short
length
()
{
return
(
short
)
memory
.
length
;
}
@Override
public
short
length
()
{
return
(
short
)
memory
.
length
;
}
}
}
src/main/java/me/felinewith/kcpu/util/BaseMemory.java
View file @
0cbce39
...
@@ -33,4 +33,6 @@ public abstract class BaseMemory implements IMemory {
...
@@ -33,4 +33,6 @@ public abstract class BaseMemory implements IMemory {
}
}
@Override
public
void
memIrq
(
short
addr
)
{
}
@Override
public
void
memIrq
(
short
addr
)
{
}
@Override
public
void
powerOff
()
{
}
}
}
test.kasm
0 → 100644
View file @
0cbce39
movs r0 0
movs r1 0
movs r2 0
movs r3 0
mov ff00,16 61,16
irqm ff01,16
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment