Advertisement
Guest User

TinySB Sub-Layer Functions

a guest
Sep 9th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. ; PROJECT : TinySBRunTime
  2. ; EDITED : 9/5/2016
  3. ; ---------------------------------------------------------------------
  4.  
  5. //NYI not yet implemented
  6. Psub parse(mystring$)
  7. endpsub
  8.  
  9. psub get_color#(color$)
  10. endpsub
  11.  
  12. //This does a few things, it draws the text page on the screen and the bg layers including ones shown/hidden
  13. psub frame_render()
  14. rendertoscreen
  15. DRAWBGS()
  16. render_TEXTPAGE()
  17. endpsub
  18.  
  19. //Draw the textpage onto the screen
  20. psub render_TEXTPAGE()
  21. drawimage text_page, 0, 0, 1
  22. endpsub
  23.  
  24. //This should be in charge of stepping the program stack foreward
  25. psub step_()
  26. inc pc //Increment the program counter
  27. endpsub
  28.  
  29. //Used for utility to resize the stack of the program
  30. psub resize_stack(value)
  31. redim program_stack$(value)
  32. endpsub
  33.  
  34. psub resize_labels(value)
  35. redim labels$(value) //the labels themselves matter
  36. redim label_pointers(value) //take care of the pointers to these are important
  37. endpsub
  38.  
  39. psub getlabelbyvalue(value)
  40. endpsub labels$(value)
  41.  
  42. psub add_returnstack(pc_pointer)
  43. //first increase the return stack size by 1
  44. inc return_stack_size
  45. //We also have to resize the return stack array now
  46. resize_Rstack(return_stack_size)
  47. return_stack(return_stack_size)=pc //the address or line number to return to after the jump
  48. pc = pc_pointer //the address or line number we are jumping to by directly modifying the pc pointer.
  49. //the logic is simple instead of parsing where we are if we see return we pop a value off the stack
  50. //and go back to the last pc address
  51. endpsub
  52.  
  53. //This should literally take care of gosub
  54. psub gosub_(label_name$)
  55. index = findlabelbystring(label_name$)
  56. if index>0
  57. add_returnstack(index)
  58. endif
  59. endpsub
  60.  
  61. psub goto_(label_name$)
  62. index = findlabelbystring(label_name$)
  63. address = label_pointers(index)
  64. if index>0
  65. jump_pc(address) //changed value to address this would have been broken
  66. endif
  67. endpsub
  68.  
  69. psub remove_returnstack()
  70. //first decrement the return stack size by 1
  71. dec return_stack_size
  72. //we have to also resize it
  73. resize_Rstack(return_stack_size)
  74. //next store the pc address from the jump below us
  75. pc = return_stack(return_stack_size)
  76. //the logic is simple, we're going back to the last place we had saved (line number)
  77. //to continue execution.
  78. //As you'd expect in this way we can have 5 or even 10 different lines to eventually return to
  79. endpsub
  80.  
  81. psub findlabelbystring(str_$)
  82. for t=0 to getarrayelements( labels$() )
  83. if str_$ = labels$(t)
  84. index=t
  85. exitfor
  86. endif
  87. next t
  88. endpsub index
  89.  
  90. psub resize_Rstack(value)
  91. redim return_stack(value)
  92. endpsub
  93.  
  94. //this takes care of jumping to the line number or index of the program to run
  95. //Probably used by goto and other commands like it
  96. psub jump_pc(value)
  97. pc = value
  98. endpsub
  99.  
  100. //utility for testing
  101. //puts an item in the program stack,
  102. //increments stack counter
  103. psub put(command$)
  104. program_stack$(program_stack_size) = command$
  105. inc program_stack_size //increments the program stack size
  106. resize_stack(program_stack_size)
  107. endpsub
  108.  
  109. Psub find_labels()
  110. dim label_strings$(1) //it should auto resize
  111. current_label = 1 //set the temporary current label counter so we actually have a label counter
  112. //pre parsing labels so we know what their line numbers are
  113. for t=1 to program_stack_size //Go through the whoollle stack
  114. entry_string$ = program_stack$(t)
  115.  
  116. //Lots of nested ifs here. basically just check the label string
  117. if left$(entry_string$, 1)="@"
  118. //Ok we found one, let's see if we can get the full string of the label we need to store it
  119. Tokens=SplitToArray(entry_string$," ",label_strings$(),1)
  120. if len(label_strings$(1))>0 and left$(label_strings$(1), 1)="@"
  121. //Just double check that the @ symbol is there.
  122.  
  123. //and this adds the label to our list
  124. add_label(label_strings$(1), t, current_label)
  125. inc current_label //don't forget to increment this or we'll over write
  126. //a previously saved or stored label
  127. //and now gosub/goto should work if called
  128.  
  129.  
  130. endif
  131.  
  132. endif
  133. next t
  134.  
  135. endpsub
  136.  
  137.  
  138. //This function handles dynamically adding new labels to the label list pre-parsing
  139. //This is never used during runtime and it better not ever be ;)
  140. psub add_label(label$, pc_index, current_label)
  141. //A little safety to make sure it doesn't break things and
  142. //that this array stays within it's indexes
  143. if current_label > label_list_size
  144. resize_labels(current_label)
  145. endif
  146. //end of safety
  147. labels$(current_label) = label$ //the actual label's name
  148. label_pointers(current_label) = pc_index //the pc address it's on
  149. endpsub
  150.  
  151. Psub parse_command(index)
  152. dim command_strings$(20)
  153. dim command_strings_$(20)
  154.  
  155. Tokens=SplitToArray(program_stack$(index),", ",command_strings$(),1)
  156.  
  157. //Parse the color command
  158. for g=1 to Tokens
  159. if command_strings$(g) ="COLOR"
  160. //Tokens=SplitToArray(command_strings$(g+1),",",command_strings_$(),1)
  161.  
  162. color_1$ = command_strings$(g+1)
  163. color_2$ = command_strings$(g+2)
  164. color_#(color_1$, color_2$)
  165. exitfor
  166. endif
  167. //Kind of parse the print command
  168. if command_strings$(g) = "Print"
  169. output_string$ = command_strings$(g+1)
  170. //locate(10, g)
  171. print_string(output_string$)
  172. endif
  173.  
  174. if command_strings$(g) = "goto"
  175. //We'll get the goto label first
  176. goto_label$ = command_strings$(g+1)
  177. //Next we're going to see if it has an @
  178. if left$(goto_label$, 1)="@"
  179. //we're dealing with a correct label
  180. goto_(goto_label$) //and this should change the pc address!
  181. exitfor //now exit the for loop
  182. endif
  183. endif
  184.  
  185. if command_strings$(g) = "locate"
  186. //we'll try to get the X and Y out of this
  187. X$ = command_strings$(g+1)
  188. Y$ = command_strings$(g+2)
  189.  
  190. //if right$(X$, 1)="," then X$=left$(X$, len(X$)-1)
  191. x = val(X$)
  192. y = val(Y$)
  193. //locate(5, 15)
  194. //print_string(X$)
  195. //locate(5, 16)
  196. //print_string(Y$)
  197. locate(x, y)// actually try to use the command
  198. //locate(20, 7)
  199. exitfor
  200. endif
  201.  
  202.  
  203.  
  204.  
  205. next g
  206.  
  207.  
  208. endpsub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement