CAT.s 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ; Copyright (C) 2016 Jeremiah Orians
  2. ; This file is part of stage0.
  3. ;
  4. ; stage0 is free software: you can redistribute it and/or modify
  5. ; it under the terms of the GNU General Public License as published by
  6. ; the Free Software Foundation, either version 3 of the License, or
  7. ; (at your option) any later version.
  8. ;
  9. ; stage0 is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. ;; CAT program
  17. ;; Concatinates multiple tapes into a single tape output
  18. ;; Read tapes in tape_01 and writes the assembled result
  19. ;; Into tape_02 and stops when user precesses C-d
  20. :start
  21. ;; Prep TAPE_02
  22. LOADUI R0 0x1101
  23. FOPEN_WRITE
  24. ;; Read_file function
  25. ;; Primary work function
  26. ;; Copies contents of TAPE_01 to TAPE_02
  27. ;; Then calls a user interaction function at EOF
  28. :Read_file
  29. ;; Prep TAPE_01
  30. LOADUI R0 0x1100
  31. FOPEN_READ
  32. :Read_Loop
  33. ;; Read Byte
  34. LOADUI R1 0x1100 ; Reading from TAPE_01
  35. FGETC ; Get a byte
  36. ;; Check for EOF
  37. CMPSKIPI.GE R0 0
  38. JUMP @Read_Cleanup
  39. ;; Write the Byte
  40. LOADUI R1 0x1101 ; Write to TAPE_02
  41. FPUTC ; That byte
  42. JUMP @Read_Loop ; Loop until EOF
  43. :Read_Cleanup
  44. ;; Close up TAPE_01
  45. LOADUI R0 0x1100
  46. FCLOSE
  47. JUMP @Prompt_User ; See if user wants to read another
  48. ;; Closeup function
  49. ;; A minimal cleanup function to ensure we end
  50. ;; In a known good state
  51. :Closeup
  52. ;; Close up TAPE_02
  53. LOADUI R0 0x1101
  54. FCLOSE
  55. HALT
  56. ;; Prompt_User function
  57. ;; Displays message to user
  58. ;; Jumps to Read_file if [ENTER]
  59. ;; Otherwise Closeup to register
  60. ;; All done reading tapes and to start closeout
  61. :Prompt_User
  62. FALSE R1 ; Using TTY
  63. FALSE R3 ; Starting at beginning
  64. LOADUI R4 $Prompt_Text ; of the prompt text
  65. :Prompt_Loop
  66. LOADXU8 R0 R3 R4 ; Get a char
  67. CMPSKIPI.NE R0 0 ; If NULL
  68. JUMP @Prompt_Done ; We reached the end
  69. FPUTC ; Write it to TTY
  70. ADDUI R3 R3 1 ; Move to next char
  71. JUMP @Prompt_Loop ; And loop again
  72. :Prompt_Done
  73. LOADUI R0 10 ; Using LF
  74. FPUTC ; Terminate Line
  75. FGETC ; Get user input
  76. ;; Check for Ctrl-D
  77. CMPSKIPI.NE R0 4 ; If user hit Ctrl-D
  78. JUMP @Closeup
  79. ;; Otherwise assume user wants to read another tape from TAPE_01
  80. JUMP @Read_file
  81. :Prompt_Text
  82. "Press [Enter] to read next tape or Ctrl-d to be done"