Skip to content

Commit

Permalink
Merge branch 'kdmmio-fix' into main
Browse files Browse the repository at this point in the history
Fixes #22
  • Loading branch information
LeoRiether committed Sep 12, 2021
2 parents ee971b5 + efafcb1 commit b861cc0
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fpgrars"
version = "1.12.1"
version = "1.13.0"
authors = ["Leonardo Riether <leonardoalves8217@gmail.com>"]
edition = "2018"

Expand Down
78 changes: 78 additions & 0 deletions samples/keydown.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#############################################
## ##
## Colors the screen if a key is pressed ##
## Uses KDMMIO_KEYDOWN, which isn't ##
## compatible with RARS ##
## ##
#############################################

.macro exit
li a7 10
ecall
.end_macro

.data

.text
main:
li s0 0x0 # color
li s1 1 # frame
li s10 0 # last pressed key, only used for get_key

main.loop:
mv a0 s0
mv a1 s1
jal print

jal is_key_down
mv s0 zero
beqz a0 not_red
li s0 0x7
not_red:

xori s1 s1 1
j main.loop

main.exit:
exit()

# a0 = color
# a1 = frame
print:
slli a2 a1 20
li t0 0xff000000
or t0 t0 a2
li t1 76800
add t1 t1 t0

slli a2 a0 8
or a0 a0 a2
slli a2 a0 16
or a0 a0 a2

print.loop:
bge t0 t1 print.exit

# li t2 -250
# print.wait:
# bgez t2 print.wait.out
# addi t2 t2 1
# j print.wait
# print.wait.out:

sw a0 0(t0)

addi t0 t0 4
j print.loop

print.exit:
li a2 0xff200604
sb a1 0(a2)
ret

is_key_down:
li t0 0xff210000
lb a0 0(t0)
lw t0 4(t0) # Clear KDMMIO_Data bit
ret

24 changes: 21 additions & 3 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ pub const HEIGHT: usize = 240;
pub const FRAME_SELECT: usize = 0x20_0604;
pub const FRAME_0: usize = 0;
pub const FRAME_1: usize = 0x10_0000;
const KEYBOARD: usize = 0x20_0000;

/// Control bit for the Keyboard (Display?) MMIO.
/// `mmio[KDMMIO_CONTROL] == 1` means that a new key has been put in `mmio[KDMMIO_DATA]`, like a
/// keydown event. The control bit will be cleared right after you read a byte/half_word/word from
/// `mmio[KDMMIO_DATA]`. `KDMMIO_KEYDOWN` is easier to use, but not supported by other simulators.
pub const KDMMIO_CONTROL: usize = 0x20_0000;
pub const KDMMIO_DATA: usize = 0x20_0004;

/// `mmio[KDMMIO_KEYDOWN] == 1` means that some key is currently down/pressed. Not supported by
/// other simulators, but switching from `0x21` to `0x20` should be easy enough.
/// `mmio[KDMMIO_DATADOWN]` is a duplicate of `mmio[KDMMIO_DATA]`
pub const KDMMIO_KEYDOWN: usize = 0x21_0000;
pub const KDMMIO_DATADOWN: usize = 0x21_0004;

const KEYBUFFER: usize = 0x20_0100;
const KEYBUFFER_SIZE: usize = 8;
const KEYMAP: usize = 0x20_0520;
Expand Down Expand Up @@ -56,8 +69,11 @@ impl InputState {

let mut mmio = state.mmio.lock().unwrap();

mmio[KEYBOARD] = 1;
mmio[KEYBOARD + 4] = chr as u8;
mmio[KDMMIO_CONTROL] = 1;
mmio[KDMMIO_DATA] = chr as u8;

mmio[KDMMIO_KEYDOWN] = 1;
mmio[KDMMIO_DATADOWN] = chr as u8;

true
}
Expand Down Expand Up @@ -102,6 +118,8 @@ impl InputState {
} => {
let mut mmio = state.mmio.lock().unwrap();

mmio[KDMMIO_KEYDOWN] = 0;

push_key_to_buffer(&mut mmio, 0xF0);
push_key_to_buffer(&mut mmio, *key as u8);

Expand Down
10 changes: 4 additions & 6 deletions src/simulator/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ use std::io::Read;
use std::sync::{Arc, Mutex};

pub const DATA_SIZE: usize = 0x0040_0000; // TODO: this, but I think it's about this much
pub const MMIO_SIZE: usize = 0x0021_0000;
pub const MMIO_SIZE: usize = 0x0022_0000;
pub const MMIO_START: usize = 0xff00_0000;
pub const KBMMIO_CONTROL: usize = 0xff20_0000;
pub const KBMMIO_DATA: usize = 0xff20_0004;

pub const HEAP_START: usize = 0x1004_0000;

use crate::renderer::{FRAME_0, FRAME_1, HEIGHT, WIDTH};
use crate::renderer::{FRAME_0, FRAME_1, HEIGHT, WIDTH, KDMMIO_CONTROL, KDMMIO_DATA};
pub const VIDEO_START: usize = MMIO_START + FRAME_0;
pub const VIDEO_END: usize = MMIO_START + FRAME_1 + WIDTH * HEIGHT;

Expand Down Expand Up @@ -74,8 +72,8 @@ impl Memory {
if i >= MMIO_START {
// MMIO
let mut mmio = self.mmio.lock().unwrap();
if i == KBMMIO_DATA {
mmio[KBMMIO_CONTROL - MMIO_START] = 0;
if i == KDMMIO_DATA + MMIO_START {
mmio[KDMMIO_CONTROL] = 0;
}
read(&mmio[i - MMIO_START..])
} else if i >= HEAP_START {
Expand Down

0 comments on commit b861cc0

Please sign in to comment.