Advertisement
Sergiovan

ASSEEEEMBLYYYYY

Aug 19th, 2014
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.03 KB | None | 0 0
  1. @DATA
  2.  
  3. DISCCOUNT DS 2 ; Counts the anmount of discs in each basket
  4. PROCESS DS 1 ; The process number
  5. DETECTED DS 1 ; The color that was detected
  6. BARRIER DS 1 ; The color the barrier is put on
  7. STOP DS 1 ; Holds if the machine should stop
  8. TIMERVAL DS 2 ; Variable to hold timer misc stuff
  9. PMW DS 1 ; Holds the PMW timer thing
  10. DISPTIME DS 1 ; Holds the Display timer thing
  11.  
  12. @CODE
  13.  
  14. IOAREA EQU -16 ; address of I/O area
  15. INPUT EQU 7 ; address of Input
  16. OUTPUT EQU 11 ; address of output
  17. TIMER EQU 13 ; address of timer
  18. INTABLE EQU 0 ; address of interrupt tables
  19. INTERRU EQU 8 ; Interrupt number
  20. ADCONVS EQU 6 ; address of the A/D converter
  21. DSPDIG EQU 9 ; address of the 7-segment digit selector
  22. DSPSEG EQU 8 ; address of the 7-segment segments
  23.  
  24. INTENSI EQU 7 ; Intensity of motors
  25.  
  26. ;Boolean values
  27. FALSE EQU 0 ; Value of false
  28. TRUE EQU 1 ; Value for true
  29.  
  30. ;Use these for colors
  31. BLACK EQU 0 ; number of Black for variables
  32. WHITE EQU 1 ; number of White for variables
  33.  
  34. ;Use these for the display
  35. DWHITE EQU %0001000 ; Segment for white counter
  36. DBLACK EQU %0100000 ; Segment for black counter
  37.  
  38. ;Use these for processes
  39. PUSHING EQU 0 ; Pusher pushing
  40. RETRACT EQU 1 ; Pusher retracting
  41. CONVEY1 EQU 2 ; First movement of conveyor belt
  42. STOPPIN EQU 3 ; Set machine to idle if nothing arrives
  43. DETECTC EQU 4 ; Detect the color for 1 sec or somezing
  44. ANALIZE EQU 5 ; Move barrier accordingly
  45. CONVEY2 EQU 6 ; Move conveyor belt again until basketing
  46. RESTART EQU 7 ; Start another cycle... OR NOT
  47.  
  48. ;Use these for inputs
  49. STARTB EQU %010000000 ; Start/Stop button
  50. ABORTB EQU %001000000 ; Abort button
  51. BLACKD EQU %000100000 ; Black disc tray detector
  52. WBUTTON EQU %000010000 ; Button that activates when the barrier is in 'white' position
  53. BBUTTON EQU %000001000 ; Button that activates when the barrier is in 'black' position
  54. LIGHTD1 EQU %000000100 ; Presence detector
  55. COLORD EQU %000000010 ; Color detector
  56. WHITED EQU %000000001 ; White disc tray detector
  57.  
  58.  
  59. ;Use these for outputs
  60. HORATIB EQU %000000001 ; Output to move horatio backwards
  61. HORATIM EQU %000000010 ; Output to move horatio forwards
  62. MOTORCB EQU %000000100 ; Motor for Conveyor Belt
  63. D1LIGHT EQU %000001000 ; Light for the presence detector
  64. MOTORBB EQU %000010000 ; Output to move the barrier towards black position
  65. MOTORBW EQU %000100000 ; Output to move the barrier towards white position
  66. DCLIGHT EQU %001000000 ; Light for the color detector
  67. DBLIGHT EQU %010000000 ; Lights for the tray detectors
  68.  
  69. ; Interrupt setup code
  70. interr_start: LOAD R2 timerrupt ; Load the adress of interrupt position
  71. ADD R2 R5 ; Add the Code Segment address
  72. LOAD R0 INTERRU ; Interrupt number
  73. MULS R0 2 ; Multiply by 2 to get the table position
  74. STOR R2 [R0] ; Save the full address in the table
  75. begin : BRA initialize ; skip subroutine Hex7Seg
  76.  
  77. ; 7SegmentDisplay method
  78. Hex7Seg : BRS Hex7Seg_bgn ; push address(tbl) onto stack and proceed at "bgn"
  79. Hex7Seg_tbl : CONS %01111110 ; 7-segment pattern for '0'
  80. CONS %00110000 ; 7-segment pattern for '1'
  81. CONS %01101101 ; 7-segment pattern for '2'
  82. CONS %01111001 ; 7-segment pattern for '3'
  83. CONS %00110011 ; 7-segment pattern for '4'
  84. CONS %01011011 ; 7-segment pattern for '5'
  85. CONS %01011111 ; 7-segment pattern for '6'
  86. CONS %01110000 ; 7-segment pattern for '7'
  87. CONS %01111111 ; 7-segment pattern for '8'
  88. CONS %01111011 ; 7-segment pattern for '9'
  89. CONS %01110111 ; 7-segment pattern for 'A'
  90. CONS %00011111 ; 7-segment pattern for 'b'
  91. CONS %01001110 ; 7-segment pattern for 'C'
  92. CONS %00111101 ; 7-segment pattern for 'd'
  93. CONS %01001111 ; 7-segment pattern for 'E'
  94. CONS %01000111 ; 7-segment pattern for 'F'
  95. Hex7Seg_bgn: AND R0 %01111 ; R0 := R0 MOD 16 , just to be safe...
  96. LOAD R3 [SP++] ; R3 := address(tbl) (retrieve from stack)
  97. LOAD R3 [R3+R0] ; R3 := tbl[R0]
  98. RTS
  99.  
  100. ;Timer interrupt code
  101. timerrupt: LOAD R1 [GB + PROCESS]
  102. ADD R1 1 ; Add 1 to process
  103. STOR R1 [GB + PROCESS]
  104. LOAD R0 0
  105. STOR R0 [R5 + OUTPUT] ; Stop ALL ZE MOTORS
  106. RTE ;Return
  107.  
  108. initialize: LOAD R0 0 ; Initialize all things to 0
  109. LOAD R5 IOAREA ; Load The IO address for relative addressing
  110. STOR R0 [GB + DISCCOUNT + BLACK] ; Discs for black to 0
  111. STOR R0 [GB + DISCCOUNT + WHITE] ; Discs for white to 0
  112. STOR R0 [GB + PROCESS] ; Process
  113. STOR R0 [GB + STOP] ; Must the machine stop
  114. STOR R0 [GB + TIMERVAL] ; Misc variables
  115. STOR R0 [GB + TIMERVAL + 1]
  116.  
  117. hor_init: LOAD R0 [R5 + ADCONVS]
  118. CMP R0 255
  119. BEQ barrier_init
  120. LOAD R0 RETRACT
  121. STOR R0 [GB + PROCESS]
  122. BRS pmw
  123. LOAD R0 PUSHING
  124. STOR R0 [GB + PROCESS]
  125. BRA hor_init
  126.  
  127. barrier_init: LOAD R1 INTENSI ; Load the intensity and set some Registers to use
  128. LOAD R2 0
  129. LOAD R4 0
  130.  
  131. barrier_check: LOAD R0 [R5 + INPUT] ; Check if any barrier buttons are in use
  132. AND R0 BBUTTON
  133. CMP R0 BBUTTON
  134. BEQ barrier_end_b ; If the button for black is pressed, end pmw in black
  135. LOAD R0 [R5 + INPUT]
  136. AND R0 WBUTTON
  137. CMP R0 WBUTTON
  138. BEQ barrier_end_w ; Same for white
  139.  
  140. barrier_loop: LOAD R0 [R5 + TIMER] ;Load the timer into R0
  141. SUB R0 10 ;Subtract 10 steps from the value of the timer
  142.  
  143. barrier_nothingloop:CMP R0 [R5 + TIMER] ;Compare the value of the timer and the desired time
  144. BMI barrier_nothingloop ; If negative, continue, else repeat
  145. barrier_pmw: CMP R2 INTENSI
  146. BLT barrier_pmw_on ; If it is less than the intensity, turn motor on
  147. BRA barrier_pmw_off ; else, turn it off
  148. barrier_pmw_off: STOR R4 [R5 + OUTPUT] ; R4 is 0, so this just turns off motors
  149. barrier_pmw_add: ADD R2 1 ; Add 1 to the pmw value
  150. CMP R2 10 ; If it gets larger than 10, put it to 0
  151. BLE barrier_check ; Else, go check if a button was pressed
  152. LOAD R2 0
  153. BRA barrier_check
  154. barrier_pmw_on: LOAD R3 MOTORBW ; Turn on the barrier motor towards white
  155. STOR R3 [R5 + OUTPUT]
  156. BRA barrier_pmw_add ; Add 1 and check for buttons
  157.  
  158. barrier_end_w: LOAD R0 WHITE ; Set the BARRIER variable to white, for later use
  159. BRA barrier_end
  160. barrier_end_b: LOAD R0 BLACK ; Set the BARRIER variable to black, for later use
  161. barrier_end: STOR R0 [GB + BARRIER] ; Save the BARRIER variable
  162. LOAD R0 0
  163. STOR R0 [R5 + OUTPUT] ; Turn off motors
  164. BRA main ; Start
  165.  
  166. main: LOAD R2 0
  167. STOR R2 [R5 + DSPDIG]
  168. LOAD R0 [R5 + INPUT] ;Check for a press of Start/Stop button
  169. AND R0 STARTB
  170. CMP R0 STARTB
  171. BEQ main_start ; After it is pressed, check for a release
  172. BRA main
  173. main_start: LOAD R0 [R5 + INPUT] ; Check for a release of said button
  174. LOAD R1 FALSE
  175. STOR R1 [GB + STOP]
  176. AND R0 STARTB
  177. CMP R0 0
  178. BEQ cycle ; After release, start cycling
  179. BRA main_start
  180.  
  181. ; Display and pmw go here
  182.  
  183. display: LOAD R2 0
  184. STOR R2 [R5 + DSPDIG]
  185. LOAD R0 [GB + DISPTIME] ;
  186. MOD R0 2000
  187. CMP R0 1000 ; digits by using modulo 2
  188. BLE display_white
  189. LOAD R0 [GB + DISCCOUNT + BLACK] ; Load DISCCOUNT black into R0 for Hex7Seg
  190. BRS Hex7Seg
  191. LOAD R2 DBLACK
  192. STOR R2 [R5 + DSPDIG]
  193. STOR R3 [R5 + DSPSEG] ; Display
  194. BRA retu
  195. display_white: LOAD R0 [GB + DISCCOUNT + WHITE] ; Same as before
  196. BRS Hex7Seg
  197. LOAD R2 DWHITE
  198. STOR R2 [R5 + DSPDIG]
  199. STOR R3 [R5 + DSPSEG]
  200. retu: LOAD R0 [GB + DISPTIME] ;
  201. ADD R0 1
  202. STOR R0 [GB + DISPTIME]
  203. RTS
  204.  
  205. pmw: LOAD R0 [GB + PMW] ; Add 1 to the PMW counter variable
  206. ADD R0 1
  207. CMP R0 10
  208. BNE pmw_continue ; If it's 10, reset to 0
  209. LOAD R0 0
  210. pmw_continue: STOR R0 [GB + PMW]
  211. LOAD R0 [GB + PROCESS] ; Load PROCESS into R0
  212. CMP R0 PUSHING ; Based on the process we turn on motors/lights
  213. BEQ pmw_0
  214. CMP R0 RETRACT
  215. BEQ pmw_1
  216. CMP R0 CONVEY1
  217. BEQ pmw_2
  218. CMP R0 ANALIZE
  219. BEQ pmw_5
  220. CMP R0 CONVEY2
  221. BEQ pmw_6
  222. RTS
  223. pmw_0: LOAD R0 0 ; For process 0 (pushing) we make horatio go forwards
  224. LOAD R1 5 ; The intensity is 5
  225. CMP R1 [GB + PMW]
  226. BLT pmw_0_on ; If PWM is higher, turn it on
  227. BRA pmw_finish ; Else, be done. This repeats for every pmw subthing
  228. pmw_0_on: LOAD R0 HORATIM
  229. BRA pmw_finish
  230. pmw_1: LOAD R0 0 ; Make horatio go backwards
  231. OR R0 D1LIGHT
  232. LOAD R1 5
  233. CMP R1 [GB + PMW]
  234. BLT pmw_1_on
  235. BRA pmw_finish
  236. pmw_1_on: LOAD R0 HORATIB
  237. OR R0 D1LIGHT
  238. BRA pmw_finish
  239. pmw_2: LOAD R0 0 ; Make the conveyor belt move, turn on presence detector light
  240. LOAD R1 1
  241. CMP R1 [GB + PMW]
  242. BLT pmw_2_on
  243. OR R0 D1LIGHT
  244. BRA pmw_finish
  245. pmw_2_on: LOAD R0 MOTORCB
  246. OR R0 D1LIGHT
  247. BRA pmw_finish
  248. pmw_5: LOAD R0 [GB + DETECTED] ; Make the barrier move depending on what
  249. CMP R0 BLACK ; disc was detected. It just has 2 processes
  250. BEQ pmw_5_black ; to move it towards black/white, but
  251. LOAD R0 0 ; does the same
  252. OR R0 DBLIGHT
  253. LOAD R1 3
  254. CMP R1 [GB + PMW]
  255. BLT pmw_5_on
  256. BRA pmw_finish
  257. pmw_5_on: LOAD R0 MOTORBB ; Turn towards white
  258. OR R0 DBLIGHT
  259. BRA pmw_finish
  260. pmw_5_black: LOAD R0 0 ; Turn towards black
  261. OR R0 DBLIGHT
  262. LOAD R1 3
  263. CMP R1 [GB + PMW]
  264. BLT pmw_5_black_on
  265. BRA pmw_finish
  266. pmw_5_black_on: LOAD R0 MOTORBW
  267. OR R0 DBLIGHT
  268. BRA pmw_finish
  269. pmw_6: LOAD R0 0 ; Move conveyor belt, turn on lights for tray detectors
  270. LOAD R1 1
  271. CMP R1 [GB + PMW]
  272. BLT pmw_6_on
  273. OR R0 DBLIGHT
  274. BRA pmw_finish
  275. pmw_6_on: LOAD R0 MOTORCB
  276. OR R0 DBLIGHT
  277. BRA pmw_finish
  278. pmw_finish: STOR R0 [R5 + OUTPUT] ; Actually start/stop the outputs
  279. RTS
  280.  
  281. set_stop: LOAD R0 TRUE
  282. STOR R0 [GB + STOP] ; Machine should stop after next full cycle
  283. RTS
  284.  
  285. abort: LOAD R2 0
  286. STOR R2 [R5 + DSPDIG]
  287. LOAD R0 0 ; Abort stops all the things and then stays here
  288. STOR R0 [R5 + OUTPUT] ; Check for Start/stop press
  289. LOAD R0 [R5 + INPUT]
  290. LOAD R4 [R5 + ADCONVS]
  291. cont: AND R0 STARTB
  292. CMP R0 STARTB
  293. BNE abort
  294. abort_loop: LOAD R0 [R5 + INPUT]
  295. AND R0 STARTB ; Check for start/stop release
  296. CMP R0 STARTB
  297. BNE initialize ; Reinitialize everything
  298. BRA abort_loop
  299.  
  300.  
  301. ;Cycle cycles through the processi
  302. cycle: LOAD R0 [R5 + INPUT] ; Check if Start/Stop was pressed to set the STOP
  303. AND R0 STARTB ; variable
  304. CMP R0 STARTB
  305. BNE set_non_stop
  306. BRS set_stop
  307. set_non_stop: LOAD R0 [R5 + INPUT] ; Check for Abort button for the same reasons
  308. AND R0 ABORTB
  309. CMP R0 ABORTB
  310. BEQ abort
  311. BRS display ; Display every iteration of cycle
  312. BRS pmw ; PMW every iteration of cycle
  313. LOAD R0 [GB + PROCESS] ; Load the PROCESS value to know where to go from
  314. CMP R0 PUSHING ; here
  315. BEQ pushing_0
  316. CMP R0 RETRACT
  317. BEQ retract_1
  318. CMP R0 CONVEY1
  319. BEQ conveyor_moving_1_2
  320. CMP R0 STOPPIN
  321. BEQ stop_3
  322. CMP R0 DETECTC
  323. BEQ detect_4
  324. CMP R0 ANALIZE
  325. BEQ analize_5
  326. CMP R0 CONVEY2
  327. BEQ conveyor_moving_2_6
  328. CMP R0 RESTART
  329. BEQ restart_7
  330. BRA cycle ; This should never be reached (And indeed it isn't)
  331.  
  332. pushing_0: LOAD R0 [GB + TIMERVAL] ; If the timer was set this process, go back
  333. CMP R0 TRUE
  334. BEQ cycle
  335. LOAD R0 0
  336. SUB R0 [R5 + TIMER]
  337. ADD R0 15000 ;Set the timer to 1.1 seconds
  338. STOR R0 [R5 + TIMER]
  339. SETI INTERRU ; Activate the interrupt and wait
  340. LOAD R0 TRUE ; Set TIMER variable so it doesn't reinitialize the timer
  341. STOR R0 [GB + TIMERVAL] ; There we go!
  342. BRA cycle
  343.  
  344. retract_1: LOAD R0 FALSE
  345. STOR R0 [GB + TIMERVAL] ; Deinitialize the TIMER variable for later use
  346. LOAD R0 [R5 + ADCONVS] ; Check the A/D converter for button presses
  347. CMP R0 255 ;
  348. BNE cycle
  349. LOAD R0 [GB + PROCESS]
  350. ADD R0 1
  351. STOR R0 [GB + PROCESS]
  352. LOAD R0 D1LIGHT
  353. STOR R0 [R5 + OUTPUT]
  354. ;retract_loop: LOAD R0 [R5 + TIMER] ;Load the timer into R0
  355. ; SUB R0 5000 ;Subtract 10 steps from the value of the timer
  356.  
  357. ;retract_nothingloop:;BRS display
  358. ; CMP R0 [R5 + TIMER] ;Compare the value of the timer and the desired time
  359. ; BMI retract_nothingloop ; If negative, continue, else repeat
  360. BRA cycle
  361.  
  362. conveyor_moving_1_2:LOAD R0 [GB + TIMERVAL]
  363. CMP R0 TRUE
  364. BEQ conv_mov_1_2_cont
  365. LOAD R0 0
  366. SUB R0 [R5 + TIMER]
  367. ADD R0 60000
  368. STOR R0 [R5 + TIMER]
  369. SETI INTERRU ; Activate the interrupt and wait
  370. LOAD R0 TRUE
  371. STOR R0 [GB + TIMERVAL]
  372. conv_mov_1_2_cont: LOAD R0 [R5 + INPUT]
  373. AND R0 LIGHTD1
  374. CMP R0 LIGHTD1
  375. BNE pro_plus_2
  376. BRA cycle
  377. pro_plus_2: LOAD R0 FALSE
  378. STOR R0 [GB + TIMERVAL]
  379. LOAD R0 [GB + PROCESS]
  380. ADD R0 2
  381. STOR R0 [GB + PROCESS]
  382. BRA cycle
  383. stop_3: LOAD R0 FALSE
  384. STOR R0 [GB + TIMERVAL]
  385. BRA initialize
  386. detect_4: LOAD R0 DCLIGHT
  387. OR R0 DBLIGHT
  388. STOR R0 [R5 + OUTPUT]
  389. LOAD R0 [GB + TIMERVAL]
  390. CMP R0 TRUE
  391. BEQ cycle
  392. LOAD R0 0
  393. SUB R0 [R5 + TIMER]
  394. ADD R0 10000
  395. STOR R0 [R5 + TIMER]
  396. SETI INTERRU ; Activate the interrupt and wait
  397. LOAD R0 TRUE
  398. STOR R0 [GB + TIMERVAL]
  399. BRA cycle
  400. analize_5: LOAD R0 [GB + TIMERVAL + 1]
  401. CMP R0 TRUE
  402. BEQ anal_5_cont
  403. LOAD R0 DBLIGHT
  404. STOR R0 [R5 + OUTPUT]
  405. LOAD R0 1
  406. STOR R0 [GB + TIMERVAL + 1]
  407. LOAD R0 [R5 + INPUT]
  408. AND R0 COLORD
  409. CMP R0 COLORD
  410. BNE anal_start_black
  411. LOAD R0 WHITE
  412. STOR R0 [GB + DETECTED]
  413. BRA anal_5_cont
  414. anal_start_black: LOAD R0 BLACK
  415. STOR R0 [GB + DETECTED]
  416. anal_5_cont: LOAD R0 [GB + DETECTED]
  417. CMP R0 [GB + BARRIER]
  418. BEQ analize_end
  419. LOAD R0 [R5 + INPUT]
  420. AND R0 BBUTTON
  421. CMP R0 BBUTTON
  422. BNE anal_white
  423. LOAD R0 [GB + DETECTED]
  424. CMP R0 BLACK
  425. BEQ anal_end_black
  426. BRA cycle
  427. anal_white: LOAD R0 [R5 + INPUT]
  428. AND R0 WBUTTON
  429. CMP R0 WBUTTON
  430. BNE cycle
  431. LOAD R0 [GB + DETECTED]
  432. CMP R0 WHITE
  433. BEQ anal_end_white
  434. BRA cycle
  435. anal_end_black: LOAD R0 BLACK
  436. STOR R0 [GB + BARRIER]
  437. BRA analize_end
  438. anal_end_white: LOAD R0 WHITE
  439. STOR R0 [GB + BARRIER]
  440. analize_end: LOAD R1 [GB + PROCESS]; Restore the process to R1
  441. ADD R1 1 ; Add 1 to process
  442. STOR R1 [GB + PROCESS]; Store in memory
  443. LOAD R0 0
  444. STOR R0 [R5 + OUTPUT] ; Stop ALL ZE MOTORS
  445. LOAD R0 DBLIGHT
  446. STOR R0 [R5 + OUTPUT]
  447. BRA cycle
  448. conveyor_moving_2_6:LOAD R0 FALSE
  449. STOR R0 [GB + TIMERVAL + 1]
  450. LOAD R0 [R5 + INPUT]
  451. AND R0 WHITED
  452. CMP R0 WHITED
  453. BNE conv_2_white
  454. LOAD R0 [R5 + INPUT]
  455. AND R0 BLACKD
  456. CMP R0 BLACKD
  457. BNE conv_2_black
  458. BRA cycle
  459. conv_2_white: LOAD R0 [GB + DISCCOUNT + WHITE]
  460. ADD R0 1
  461. STOR R0 [GB + DISCCOUNT + WHITE]
  462. BRA conv_2_end
  463. conv_2_black: LOAD R0 [GB + DISCCOUNT]
  464. ADD R0 1
  465. STOR R0 [GB + DISCCOUNT]
  466. BRA conv_2_end
  467. conv_2_end: LOAD R1 [GB + PROCESS]; Restore the process to R1
  468. ADD R1 1 ; Add 1 to process
  469. STOR R1 [GB + PROCESS]; Store in memory
  470. LOAD R0 0
  471. STOR R0 [R5 + OUTPUT] ; Stop ALL ZE MOTORS
  472. BRA cycle
  473. restart_7: LOAD R0 0
  474. STOR R0 [GB + PROCESS]
  475. STOR R0 [GB + TIMERVAL]
  476. STOR R0 [GB + TIMERVAL + 1]
  477. LOAD R0 [GB + STOP]
  478. CMP R0 TRUE
  479. BEQ main
  480. BRA cycle
  481. @END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement