Public paste
typing
By: microhaxo | Date: Jul 4 2009 00:22 | Format: None | Expires: never | Size: 2.86 KB | Hits: 865

  1. .begin
  2.  
  3. ! Constants
  4. BASE                    .equ 0x3fffc0   ! Starting point of the memory mapped region
  5. CONSOLE                 .equ 0x0                ! 0xffff0000 Console Data Port
  6. CONSOLE_STATUS  .equ 0x4                ! 0xffff0004 Console Status Port (-0x80 = Not Ready)
  7. KEYBOARD                .equ 0x8                ! 0xffff0008 Keyboard in port
  8. KEYBOARD_STATUS .equ 0xc                ! 0xffff000c Keyboard status port
  9.  
  10. ! Push the contents of a register onto the stack.
  11. .macro PUSH arg1
  12.         st arg1, %r14                           ! Store the value
  13.         sub %r14, 4, %r14                       ! Decrement the stack pointer
  14. .endmacro
  15.  
  16. ! Pop whatever is on the top of the stack and put it in arg1
  17. .macro POP arg1
  18.         add %r14, 4, %r14                       ! Incremement the stack
  19.         ld %r14, arg1                           ! Get the value
  20.         st %r0, %r14                            ! Clear it out - makes it easier to debg
  21. .endmacro
  22.  
  23. .macro RETURN
  24.         jmpl %r15 + 4, %r0
  25. .endmacro
  26.  
  27.  
  28. !-------------------------------------------------------
  29. ! Start
  30. !-------------------------------------------------------
  31.         .org 2048
  32.  
  33.                 ld [SP], %r14           ! Load the address of our string onto the stack
  34.  
  35. MAIN_LOOP:
  36.                 call READ_CHAR
  37.                 POP %r1
  38.                 subcc %r1, 0x1B, %r0    ! Compare with ESC
  39.                 be QUIT
  40.  
  41.                 PUSH %r1
  42.                 call PRINT_CHAR
  43.                 ba MAIN_LOOP
  44. QUIT:
  45.                 halt
  46. !-------------------------------------------------------
  47.  
  48.  
  49.  
  50. !-------------------------------------------------------
  51. ! READ_CHAR FUNCTION
  52. ! Reads a single char from the input console
  53. ! RETURN = Char Found
  54. !-------------------------------------------------------
  55. READ_CHAR:
  56.                                                                                         ! We never call out, so %r15 is safe- don't need to store it
  57.  
  58.         add %r0, %r0, %r4                                               ! %r4 Holds the base address
  59.         sethi BASE, %r4                                                 !
  60.  
  61. READ_CHAR_WAIT:        
  62.                 ldub [%r4 + KEYBOARD_STATUS], %r1       ! Get the status
  63.                 andcc %r1, 0x80, %r1                            ! Add 0x80 - if we're ready, this is not zero
  64.                 be READ_CHAR_WAIT                                       ! Loop
  65.  
  66.                 ldub [%r4 + KEYBOARD], %r3                      ! Get the char
  67.  
  68.         PUSH %r3                                                                ! Push the result on the stack and...
  69.         RETURN                                                                  ! Return
  70. !-------------------------------------------------------
  71.  
  72. !-------------------------------------------------------
  73. ! PRINT_CHAR FUNCTION
  74. ! Prints a single char to the output console
  75. ! ARG1 = Char to print
  76. ! RETURN = Nothing
  77. !-------------------------------------------------------
  78. PRINT_CHAR:
  79.         POP %r1                                                         ! Grab the char to print
  80.                                                                                 ! We never call out, so we can assume %r15 stays put
  81.  
  82.         add %r0, %r0, %r4                                       ! %r4 Holds the base address
  83.         sethi BASE, %r4                                         !
  84.  
  85. PRINT_CHAR_WAIT:                                                ! We have to wait for the console to be ready!
  86.         ldub [%r4+ CONSOLE_STATUS], %r5
  87.                                                                                 ! Load the console status (as an unsigned byte) into %r5
  88.         andcc %r5, 0x80, %r5                            ! -0x80 = no console not ready, so add 0x80 to the value and compare against zero
  89.         be PRINT_CHAR_WAIT                                      ! Loop while we're not ready
  90.  
  91.         stb %r1, [%r4 + CONSOLE]                        ! We're ready - put the char to print into the console memory location
  92.        
  93.         RETURN
  94. !-------------------------------------------------------
  95.  
  96. SP: 0x7FFFFFFC         
  97.  
  98.         .end