Sunday, February 19, 2023

MARS MIPS - A Program to echo output from the Keyboard


Introduction

The aim to write a program that demonstrates the use of peripheral devices like keyboard. The program should echo what is being getting typed in the keyboard. The post describes the code that generates the output shown in the video.

This is me practicing basic stuff to be done in the MIPS assembly language program.


Code Section

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Module 10 Memory Mapped I/O Demo

# Memory mapped address of device registers.

# 0xFFFF0000 rcv contrl

# 0xFFFF0004 rcv data

# 0xFFFF0008 tx contrl

# 0xFFFF000c tx data        
.data

.eqv    MMIOBASE    0xffff0000

# Receiver Control Register (Ready Bit)
.eqv    RCR_        0x0000
.eqv    RCR         RCR_($s0)

# Receiver Data Register (Key Pressed - ASCII)
.eqv    RDR_        0x0004
.eqv    RDR         RDR_($s0)

# Transmitter Control Register (Ready Bit)
.eqv    TCR_        0x0008
.eqv    TCR         TCR_($s0)

# Transmitter Data Register (Key Displayed- ASCII)
.eqv    TDR_        0x000c
.eqv    TDR         TDR_($s0)
.text
.globl  main

main:
    li      $s0,MMIOBASE            # get base address of MMIO area

keyWait:
    lw      $t0,RCR                 # get control reg
    andi    $t0,$t0,1               # isolate ready bit
    beq     $t0,$zero,keyWait       # is key available? if no, loop

    lbu     $a0,RDR                 # get key value

displayWait:
    lw      $t1,TCR                 # get control reg
    andi    $t1,$t1,1               # isolate ready bit
    beq     $t1,$zero,displayWait   # is display ready? if no, loop

    sb      $a0,TDR                 # send key to display

    li      $v0,11
    syscall

    j       keyWait

Here's a high-level overview of what the code is doing:

  1. It defines memory-mapped addresses for four registers: Receiver Control Register (RCR), Receiver Data Register (RDR), Transmitter Control Register (TCR), and Transmitter Data Register (TDR).

  2. In the main function, it initializes a register $s0 with the MMIO base address.

  3. It enters a loop that waits for a keypress by checking the value of the ready bit in the RCR register. Once a keypress is detected, the code retrieves the ASCII code of the key from the RDR register.

  4. The code then enters another loop that waits for the display to be ready to receive data by checking the value of the ready bit in the TCR register. Once the display is ready, the code sends the ASCII code of the key to the display by storing it in the TDR register.

  5. Finally, the code calls the syscall function to display the key on the screen and then jumps back to the beginning of the loop to wait for the next keypress.


Conclusion

This assembly language program that demonstrates Memory-Mapped I/O (MMIO). MMIO is a method of communication between the CPU and peripheral devices where memory locations are used to represent the device's control and data registers.

No comments:

Post a Comment