Advertisement
yugorin

Chapter 15

Feb 27th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. :BasicUpstart2(main)
  2. //* = $2000
  3. //----------------------------------------------------------
  4. // Code for creating the breakpoint file sent to Vice.
  5. //----------------------------------------------------------
  6. .var _useBinFolderForBreakpoints = cmdLineVars.get("usebin") == "true"
  7. .var _createDebugFiles = cmdLineVars.get("afo") == "true"
  8. .print "File creation " + [_createDebugFiles
  9.     ? "enabled (creating breakpoint file)"
  10.     : "disabled (no breakpoint file created)"]
  11. .var brkFile
  12. .if(_createDebugFiles) {
  13.     .if(_useBinFolderForBreakpoints)
  14.         .eval brkFile = createFile("bin/breakpoints.txt")
  15.     else
  16.         .eval brkFile = createFile("breakpoints.txt")
  17. }
  18. .macro break() {
  19. .if(_createDebugFiles) {
  20.     .eval brkFile.writeln("break " + toHexString(*))
  21.     }
  22. }
  23. //------------------------------------------------------
  24.  
  25.  
  26. .const BORDER = $d020
  27.  
  28. main:
  29. dword_equality_assertion_works:
  30.   :assert_dwords_equal(dword_a, dword_a)
  31.   :assert_dwords_equal(dword_b, dword_b)
  32.   :assert_dwords_equal(dword_c, dword_c)
  33.   :assert_dwords_equal(dword_d, dword_d)
  34.   :assert_dwords_equal(dword_e, dword_e)
  35.  
  36. // TODO: a) uncoment temporarily asserts below (one at the time) to check if the assert fails when it should
  37. //  :assert_dwords_equal(dword_a, dword_b) // this should fail
  38. //  :assert_dwords_equal(dword_a, dword_c) // this should fail
  39. //  :assert_dwords_equal(dword_a, dword_d) // this should fail
  40. //  :assert_dwords_equal(dword_a, dword_e) // this should fail
  41.  
  42. dword_subtraction_works:
  43. :break()
  44.  
  45.   clc
  46.   :subtract_dwords(number_a, number_b, result)
  47. // TODO: b) uncomment asserts below if all previous tests are green
  48.   :assert_dwords_equal(expected_result, result)
  49.  
  50.  
  51. dword_inequality_assertion_works:
  52. // TODO: c) uncomment asserts below if the tests are green
  53.  :assert_dwords_not_equal(dword_a, dword_b)
  54.  :assert_dwords_not_equal(dword_a, dword_c)
  55.  :assert_dwords_not_equal(dword_a, dword_d)
  56.  :assert_dwords_not_equal(dword_a, dword_e)
  57.  
  58. // TODO: c) uncoment temporarily asserts below (one at the time) to check if the assert fails when it should
  59. //  :assert_dwords_not_equal(dword_a, dword_a) // this should fail
  60. //  :assert_dwords_not_equal(dword_b, dword_b) // this should fail
  61. //  :assert_dwords_not_equal(dword_c, dword_c) // this should fail
  62. //  :assert_dwords_not_equal(dword_d, dword_d) // this should fail
  63. //  :assert_dwords_not_equal(dword_e, dword_e) // this should fail
  64.  
  65.  
  66.  
  67. render_test_result:
  68.   lda test_result
  69.   sta BORDER
  70.   rts
  71.  
  72. tests_fail:
  73.   lda #RED
  74.   sta test_result
  75.   jmp render_test_result
  76.  
  77. test_result:
  78.   .byte GREEN
  79.  
  80.  
  81. number_a:
  82.   .dword $11223344
  83.  
  84. number_b:
  85.   .dword $01020304
  86.  
  87. result:
  88.   .dword $0
  89.  
  90. expected_result:
  91.   .dword $11223344 - $01020304
  92.  
  93. dword_a:
  94.   .dword $11111111
  95. dword_b:
  96.   .dword $00111111
  97. dword_c:
  98.   .dword $11001111
  99. dword_d:
  100.   .dword $11110011
  101. dword_e:
  102.   .dword $11111100
  103.  
  104. .macro assert_dwords_equal(expected, actual) {
  105.     // TODO: a). implement this assertion
  106.   .var bytes = 4
  107.   .for(var byte = 0; byte < bytes; byte++) {
  108.     lda actual + byte
  109.     cmp expected + byte
  110.     bne fail
  111.   }
  112.   jmp pass
  113. fail:
  114.     jmp tests_fail
  115. pass:
  116. }
  117.  
  118.  
  119. .macro subtract_dwords(a, b, result) {
  120.     :subtract_integers(32, a, b, result)
  121. }
  122.  
  123.  
  124. .macro subtract_integers(bits, a, b, result) {
  125.   .var bytes = bits_to_bytes(bits)
  126.    
  127.       .for(var byte = 0; byte < bytes; byte = byte + 1) {
  128.     :subtract_bytes_with_offset(byte, a, b, result)
  129.   }
  130. }
  131.  
  132. .macro subtract_bytes_with_offset(offset, a, b, result) {
  133.     cld
  134.     sec
  135.     lda a + offset
  136.     sbc b + offset
  137.     sta result + offset
  138. }
  139.  
  140.  
  141. .macro assert_dwords_not_equal(expected, actual) {
  142.   // TODO: c) implement this macro
  143.   .var bytes = 4
  144.   .for(var byte = 0; byte < bytes; byte++) {
  145.     lda actual + byte
  146.     cmp expected + byte
  147.     bne pass
  148.   }
  149. fail:
  150.     jmp tests_fail
  151. pass:
  152. }
  153.  
  154.  
  155.  
  156. .macro assert_integers_equal(bits, expected, actual) {
  157.   .var bytes = bits_to_bytes(bits)
  158.  
  159.   .for(var byte = 0; byte < bytes; byte++) {
  160.     lda actual + byte
  161.     cmp expected + byte
  162.     bne fail
  163.   }
  164.     jmp pass
  165.   fail:
  166.     jmp tests_fail
  167.   pass:
  168. }
  169.  
  170. .function bits_to_bytes(bits) {
  171.   .return bits / 8
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement