Implements display view

This commit is contained in:
Felipe Lima 2015-06-22 00:25:33 -07:00
parent 4839b4b52f
commit 3c25abc55b
8 changed files with 75 additions and 10 deletions

View File

@ -1,6 +1,58 @@
package android.emu6502 package android.emu6502
final class Display { import android.content.Context
fun updatePixel(addr: Int) { import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Point
import android.util.AttributeSet
import android.util.Log
import android.view.View
import java.util.ArrayList
open class Display : View {
private val numX = 32
private val numY = 32
private val palette = arrayOf(
"#000000", "#ffffff", "#880000", "#aaffee",
"#cc44cc", "#00cc55", "#0000aa", "#eeee77",
"#dd8855", "#664400", "#ff7777", "#333333",
"#777777", "#aaff66", "#0088ff", "#bbbbbb")
private val drawingCache = ArrayList<Pixel>()
private val paint: Paint
private val bgPaint: Paint
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
paint = Paint()
bgPaint = Paint()
bgPaint.setColor(Color.BLACK);
}
open fun updatePixel(addr: Int, value: Int) {
val color = palette[value]
val x = (addr - 0x200) % 32
val y = Math.floor(((addr - 0x200) / 32).toDouble())
drawingCache.add(Pixel(Point(x.toInt(), y.toInt()), Color.parseColor(color)))
invalidate()
}
override fun onDraw(canvas: Canvas) {
val pixelSize = getWidth() / numX
canvas.drawRect(0f, 0f, getWidth().toFloat(), getHeight().toFloat(), bgPaint)
for (pixel in drawingCache) {
val right = (pixel.point.x * pixelSize).toFloat()
val top = (pixel.point.y * pixelSize).toFloat()
paint.setColor(pixel.color)
canvas.drawRect(right, top, right + pixelSize, top + pixelSize, paint)
}
drawingCache.clear()
}
class Pixel(val point: Point, val color: Int) {
} }
} }

View File

@ -2,8 +2,7 @@ package android.emu6502
import android.emu6502.instructions.Symbols import android.emu6502.instructions.Symbols
final class Emulator { final class Emulator(display: Display) {
val display = Display()
val memory = Memory(display) val memory = Memory(display)
val cpu = CPU(memory) val cpu = CPU(memory)
val assembler = Assembler(memory, Symbols()) val assembler = Assembler(memory, Symbols())

View File

@ -15,8 +15,8 @@ class Memory(private val display: Display) {
fun storeByte(addr: Int, value: Int) { fun storeByte(addr: Int, value: Int) {
set(addr, value.and(0xff)) set(addr, value.and(0xff))
if ((addr >= 0x200) && (addr <= 0x5ff)) { if (addr >= 0x200 && addr <= 0x5ff) {
display.updatePixel(addr) display.updatePixel(addr, mem[addr].and(0x0f))
} }
} }

View File

@ -1,5 +1,6 @@
package android.emu6502.app package android.emu6502.app
import android.emu6502.Display
import android.emu6502.Emulator import android.emu6502.Emulator
import android.emu6502.R import android.emu6502.R
import android.os.Bundle import android.os.Bundle
@ -9,6 +10,7 @@ import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar import android.support.v7.widget.Toolbar
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import butterknife.bindView import butterknife.bindView
@ -22,9 +24,9 @@ public class MainActivity : AppCompatActivity() {
val txtSP: TextView by bindView(R.id.SP) val txtSP: TextView by bindView(R.id.SP)
val txtPC: TextView by bindView(R.id.PC) val txtPC: TextView by bindView(R.id.PC)
val txtFlags: TextView by bindView(R.id.PC) val txtFlags: TextView by bindView(R.id.PC)
val display: Display by bindView(R.id.display)
val txtInstructions: TextView by bindView(R.id.txtInstructions) val txtInstructions: TextView by bindView(R.id.txtInstructions)
val fabRun: FloatingActionButton by bindView(R.id.fabRun) val fabRun: FloatingActionButton by bindView(R.id.fabRun)
val emulator = Emulator()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -36,6 +38,8 @@ public class MainActivity : AppCompatActivity() {
ab.setDisplayHomeAsUpEnabled(true) ab.setDisplayHomeAsUpEnabled(true)
fabRun.setOnClickListener { fabRun.setOnClickListener {
display.setVisibility(View.VISIBLE)
val emulator = Emulator(display)
emulator.assembler.assembleCode(txtInstructions.getText().toString().splitBy("\n")) emulator.assembler.assembleCode(txtInstructions.getText().toString().splitBy("\n"))
Toast.makeText(fabRun.getContext(), Toast.makeText(fabRun.getContext(),
"Code assembled successfully, " + emulator.assembler.codeLen + " bytes.", "Code assembled successfully, " + emulator.assembler.codeLen + " bytes.",

View File

@ -44,6 +44,12 @@
android:textSize="14sp" android:textSize="14sp"
android:gravity="top" /> android:gravity="top" />
<android.emu6502.Display
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</android.support.v7.widget.CardView> </android.support.v7.widget.CardView>
<LinearLayout <LinearLayout

View File

@ -11,13 +11,14 @@ import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
public class AssemblerTest { public class AssemblerTest {
private Assembler assembler; private Assembler assembler;
@Before public void setUp() { @Before public void setUp() {
assembler = new Assembler(new Memory(new Display()), new Symbols()); assembler = new Assembler(new Memory(mock(Display.class)), new Symbols());
} }
@Test public void testSimple() { @Test public void testSimple() {

View File

@ -11,6 +11,7 @@ import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
public class CPUTest { public class CPUTest {
@ -18,7 +19,8 @@ public class CPUTest {
private Assembler assembler; private Assembler assembler;
@Before public void setUp() { @Before public void setUp() {
Memory memory = new Memory(new Display());
Memory memory = new Memory(mock(Display.class));
assembler = new Assembler(memory, new Symbols()); assembler = new Assembler(memory, new Symbols());
cpu = new CPU(memory); cpu = new CPU(memory);
} }

View File

@ -9,6 +9,7 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
public class LabelsTest { public class LabelsTest {
@ -17,7 +18,7 @@ public class LabelsTest {
@Before public void setUp() { @Before public void setUp() {
Symbols symbols = new Symbols(); Symbols symbols = new Symbols();
assembler = new Assembler(new Memory(new Display()), symbols); assembler = new Assembler(new Memory(mock(Display.class)), symbols);
labels = new Labels(assembler, symbols); labels = new Labels(assembler, symbols);
} }