Advertisement
Guest User

transfer.h

a guest
Jan 18th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FindData:
  2.     ld hl, transData
  3.     ret
  4. FindDataCompressed:
  5.     ld hl, transDataCompressed
  6.     ret
  7.  
  8. ReceiveDataSafe:
  9.     call ReceiveData
  10.     call FindData
  11.     ex de,hl
  12.     ld hl, receiveDataSafeData1
  13.     call CopyDEStringtoHL
  14.    
  15.     call ReceiveData
  16.     call FindData
  17.     ex de,hl
  18.     ld hl, receiveDataSafeData2
  19.     call CopyDEStringtoHL
  20.    
  21.     call ReceiveData
  22.     call FindData
  23.     ex de,hl
  24.     ld hl, receiveDataSafeData3
  25.     call CopyDEStringtoHL
  26.     call CopyDataAndRepair
  27.    
  28.     ret
  29.    
  30. ReceiveData:
  31.     ld a, 0
  32.     ld (transInc), a
  33.    
  34. ReceiveDataLoop:
  35.     call ReceiveBit
  36.     cp 255
  37.     ret z
  38.    
  39.     ld b, a
  40.     ld a, (transInc)
  41.     ld hl, transData
  42.     add a, l
  43.     ld l, a
  44.     ld a, b
  45.     add a, 48
  46.     ld (hl), a
  47.     ld a, (transInc)
  48.     inc a
  49.     ld (transInc), a
  50.  
  51.     jp ReceiveDataLoop
  52.  
  53.     ret
  54.  
  55. ReceiveBit:
  56.     in a,(0)
  57.     ld (prev), a        ; store the current state
  58.    
  59. Receive_Loop:
  60.     in a,(0)            ; get a new state
  61.     ld b, a             ; new -> b
  62.     ld a, (prev)        ; old -> a
  63.     cp b                ; if new == old then
  64.     jp z, Receive_Loop  ; loop
  65.    
  66.     ld (tmpa), a
  67.     ld a, b
  68.     ld (tmpb), a
  69.    
  70.     ld a, (tmpb)
  71.     ld b, a
  72.     ld a, (tmpa)
  73.     call r1
  74.     ld a, (tmpb)
  75.     ld b, a
  76.     ld a, (tmpa)
  77.     call r2
  78.     ld a, (tmpb)
  79.     ld b, a
  80.     ld a, (tmpa)
  81.     call r3
  82.     ld a, (tmpb)
  83.     ld b, a
  84.     ld a, (tmpa)
  85.     call r4
  86.     ld a, (tmpb)
  87.     ld b, a
  88.     ld a, (tmpa)
  89.     call r5
  90.     ld a, (tmpb)
  91.     ld b, a
  92.     ld a, (tmpa)
  93.     call r6
  94.     ld a, (tmpb)
  95.     ld b, a
  96.     ld a, (tmpa)
  97.     call r7
  98.     ld a, (tmpb)
  99.     ld b, a
  100.     ld a, (tmpa)
  101.     call r8
  102.     ld a, (tmpb)
  103.     ld b, a
  104.     ld a, (tmpa)
  105.     call r9
  106.     ld a, (tmpb)
  107.     ld b, a
  108.     ld a, (tmpa)
  109.     call rfin
  110.  
  111.     ld a, (fin)
  112.     ret
  113.    
  114. r1:
  115.  
  116.     ; 0 -> 1
  117.     cp 0        ; if old != 0
  118.     ret nz      ; then skip this part
  119.     ld c, a     ; old -> c
  120.    
  121.     ld a, b     ; new -> a
  122.     ld b, c     ; old -> b
  123.     cp 1        ; if new != 1
  124.     ret nz      ; then skip this part
  125.    
  126.     ld a, 0     ; return 0
  127.     ld (fin), a
  128.    
  129.     ret
  130.    
  131. r2:
  132.  
  133.     ; 0 -> 2
  134.     cp 0        ; if old != 0
  135.     ret nz      ; then skip this part
  136.     ld c, a     ; old -> c
  137.    
  138.     ld a, b     ; new -> a
  139.     ld b, c     ; old -> b
  140.     cp 2        ; if new != 2
  141.     ret nz      ; then skip this part
  142.    
  143.     ld a, 1     ; return 1
  144.     ld (fin), a
  145.    
  146.     ret
  147.    
  148. r3:
  149.  
  150.     ; 1 -> 0
  151.     cp 1        ; if old != 1
  152.     ret nz      ; then skip this part
  153.     ld c, a     ; old -> c
  154.    
  155.     ld a, b     ; new -> a
  156.     ld b, c     ; old -> b
  157.     cp 0        ; if new != 0
  158.     ret nz      ; then skip this part
  159.    
  160.     ld a, 0     ; return 0
  161.     ld (fin), a
  162.    
  163.     ret
  164.    
  165.    
  166. r4:
  167.  
  168.     ; 1 -> 2
  169.     cp 1        ; if old != 1
  170.     ret nz      ; then skip this part
  171.     ld c, a     ; old -> c
  172.    
  173.     ld a, b     ; new -> a
  174.     ld b, c     ; old -> b
  175.     cp 2        ; if new != 2
  176.     ret nz      ; then skip this part
  177.    
  178.     ld a, 1     ; return 0
  179.     ld (fin), a
  180.    
  181.     ret
  182.    
  183.    
  184. r5:
  185.  
  186.     ; 2 -> 0
  187.     cp 2        ; if old != 2
  188.     ret nz      ; then skip this part
  189.     ld c, a     ; old -> c
  190.    
  191.     ld a, b     ; new -> a
  192.     ld b, c     ; old -> b
  193.     cp 0        ; if new != 0
  194.     ret nz      ; then skip this part
  195.    
  196.     ld a, 0     ; return 0
  197.     ld (fin), a
  198.    
  199.     ret
  200.    
  201.    
  202. r6:
  203.  
  204.     ; 2 -> 1
  205.     cp 2        ; if old != 2
  206.     ret nz      ; then skip this part
  207.     ld c, a     ; old -> c
  208.    
  209.     ld a, b     ; new -> a
  210.     ld b, c     ; old -> b
  211.     cp 1        ; if new != 1
  212.     ret nz      ; then skip this part
  213.    
  214.     ld a, 1     ; return 1
  215.     ld (fin), a
  216.    
  217.     ret
  218.    
  219. r7:
  220.  
  221.     ; 3 -> 1
  222.     cp 3        ; if old != 3
  223.     ret nz      ; then skip this part
  224.     ld c, a     ; old -> c
  225.    
  226.     ld a, b     ; new -> a
  227.     ld b, c     ; old -> b
  228.     cp 1        ; if new != 1
  229.     ret nz      ; then skip this part
  230.    
  231.     ld a, 0     ; return 0
  232.     ld (fin), a
  233.    
  234.     ret
  235.      
  236. r8:
  237.  
  238.     ; 3 -> 2
  239.     cp 3        ; if old != 3
  240.     ret nz      ; then skip this part
  241.     ld c, a     ; old -> c
  242.    
  243.     ld a, b     ; new -> a
  244.     ld b, c     ; old -> b
  245.     cp 2        ; if new != 2
  246.     ret nz      ; then skip this part
  247.    
  248.     ld a, 1     ; return 1
  249.     ld (fin), a
  250.    
  251.     ret
  252.          
  253. r9:
  254.  
  255.     ; 3 -> 0
  256.     cp 3        ; if old != 3
  257.     ret nz      ; then skip this part
  258.     ld c, a     ; old -> c
  259.    
  260.     ld a, b     ; new -> a
  261.     ld b, c     ; old -> b
  262.     cp 0        ; if new != 0
  263.     ret nz      ; then skip this part
  264.    
  265.     ld a, -1    ; return -1
  266.     ld (fin), a
  267.    
  268.     ret
  269.    
  270.    
  271. rfin:
  272.     ld a, b     ; if new != 3
  273.     cp 3        ; then skip this part
  274.     ret nz
  275.    
  276.    
  277.     ld a, -1    ; return -1
  278.     ld (fin), a
  279.    
  280.     ret        
  281.    
  282. ResetCable:
  283.     ld a, 0
  284.     out (0),a
  285.     ret
  286.  
  287.  
  288. SendDataSafe:
  289.     call SendData
  290.     call pause
  291.     call SendData
  292.     call pause
  293.     call SendData
  294.     call pause
  295.     ret
  296.  
  297. SendData:
  298.     ld a, 0
  299.     ld (transSendPrev), a
  300.     ld (transInc),a
  301.     call ResetCable
  302.    
  303. SendData_Loop:
  304.     ld hl, transData
  305.     ld a, (transInc)
  306.     add a, l
  307.     ld l, a
  308.    
  309.     ld a, (hl)
  310.     cp 0
  311.     jp z, SendData_Next
  312.    
  313.     sub 48
  314.     call SendBit
  315.    
  316.    
  317.     ld a, (transInc)
  318.     inc a
  319.     ld (transInc), a
  320.     jp SendData_Loop
  321.    
  322. SendData_Next:
  323.     call ResetCable
  324.     ret
  325.    
  326.     call ResetCable
  327.     ld a, 0
  328.     call SendBit
  329.     ld a, 1
  330.     call SendBit
  331.     ld a, 0
  332.     call SendBit
  333.     ld a, 1
  334.     call SendBit
  335.     ld a, 0
  336.     call SendBit
  337.     ld a, 1
  338.     call SendBit
  339.     ld a, 0
  340.     call SendBit
  341.     ld a, 0
  342.     call SendBit
  343.     call ResetCable
  344.    
  345.     ret
  346.    
  347. SendBit:
  348.  
  349.     cp 0
  350.     call z, send0
  351.     cp 1
  352.     call z, send1
  353.     call pause
  354.    
  355.     ret
  356.    
  357.  
  358. send0:
  359.     ld a, (transSendPrev)
  360.     cp 0
  361.     call z,send0_0
  362.     ld a, (transSendPrev)
  363.     cp 1
  364.     call z,send0_1
  365.     ld a, (transSendPrev)
  366.     cp 2
  367.     call z,send0_2
  368.     ld a, (transSendPrev)
  369.     cp 3
  370.     call z,send0_3
  371.  
  372.     ret
  373.  
  374. send0_0:
  375.     ld a, 2
  376.     ld (transSendPrev), a
  377.     out (0),a
  378.     ret
  379. send0_1:
  380.     ld a, 3
  381.     ld (transSendPrev), a
  382.     out (0),a
  383.     ret
  384. send0_2:
  385.     ld a, 3
  386.     ld (transSendPrev), a
  387.     out (0),a
  388.     ret
  389. send0_3:
  390.     ld a, 2
  391.     ld (transSendPrev), a
  392.     out (0),a
  393.     ret
  394.  
  395. send1:
  396.     ld a, (transSendPrev)
  397.     cp 0
  398.     call z,send1_0
  399.     ld a, (transSendPrev)
  400.     cp 1
  401.     call z,send1_1
  402.     ld a, (transSendPrev)
  403.     cp 2
  404.     call z,send1_2
  405.     ld a, (transSendPrev)
  406.     cp 3
  407.     call z,send1_3
  408.  
  409.     ret
  410.  
  411. send1_0:
  412.     ld a, 1
  413.     ld (transSendPrev), a
  414.     out (0),a
  415.     ret
  416. send1_1:
  417.     ld a, 2
  418.     ld (transSendPrev), a
  419.     out (0),a
  420.     ret
  421. send1_2:
  422.     ld a, 1
  423.     ld (transSendPrev), a
  424.     out (0),a
  425.     ret
  426. send1_3:
  427.     ld a, 1
  428.     ld (transSendPrev), a
  429.     out (0),a
  430.     ret
  431.  
  432.  
  433. CompressData:
  434.     ld b, 0
  435.     ld a, (transData)
  436.     cp \'1\'
  437.     call z,set7
  438.     ld a, (transData+1)
  439.     cp \'1\'
  440.     call z,set6
  441.     ld a, (transData+2)
  442.     cp \'1\'
  443.     call z,set5
  444.     ld a, (transData+3)
  445.     cp \'1\'
  446.     call z,set4
  447.     ld a, (transData+4)
  448.     cp \'1\'
  449.     call z,set3
  450.     ld a, (transData+5)
  451.     cp \'1\'
  452.     call z,set2
  453.     ld a, (transData+6)
  454.     cp \'1\'
  455.     call z,set1
  456.     ld a, (transData+7)
  457.     cp \'1\'
  458.     call z,set0
  459.    
  460.     ld a, b
  461.     ld (transDataCompressed), a
  462.     ret
  463.  
  464. set7:
  465.     ld a, %10000000
  466.     or b
  467.     ld b, a
  468.     ret
  469. set6:
  470.     ld a, %01000000
  471.     or b
  472.     ld b, a
  473.     ret
  474. set5:
  475.     ld a, %00100000
  476.     or b
  477.     ld b, a
  478.     ret
  479. set4:
  480.     ld a, %00010000
  481.     or b
  482.     ld b, a
  483.     ret
  484. set3:
  485.     ld a, %00001000
  486.     or b
  487.     ld b, a
  488.     ret
  489. set2:
  490.     ld a, %00000100
  491.     or b
  492.     ld b, a
  493.     ret
  494. set1:
  495.     ld a, %00000010
  496.     or b
  497.     ld b, a
  498.     ret
  499. set0:
  500.     ld a, %00000001
  501.     or b
  502.     ld b, a
  503.     ret
  504.  
  505.  
  506.  
  507.    
  508. CopyDataAndRepair:
  509.     ld a, 0
  510.     ld (CopyDataAndRepairIncr), a
  511.  
  512. CopyDataAndRepairLoop:
  513.    
  514.     ld hl, receiveDataSafeData1
  515.     ld a, (CopyDataAndRepairIncr)
  516.     add a, l
  517.     ld l, a
  518.     ld a, (hl)
  519.     cp 0
  520.     ret z
  521.     cp \'1\'
  522.     jp z, CopyDataAndRepairNext
  523.    
  524.     ld hl, receiveDataSafeData2
  525.     ld a, (CopyDataAndRepairIncr)
  526.     add a, l
  527.     ld l, a
  528.     ld a, (hl)
  529.     cp \'1\'
  530.     jp z, CopyDataAndRepairNext
  531.    
  532.     ld hl, receiveDataSafeData3
  533.     ld a, (CopyDataAndRepairIncr)
  534.     add a, l
  535.     ld l, a
  536.     ld a, (hl)
  537.     cp \'1\'
  538.     jp z, CopyDataAndRepairNext
  539.    
  540.     ld hl, transData
  541.     ld a, (CopyDataAndRepairIncr)
  542.     add a, l
  543.     ld l, a
  544.     ld (hl),\'0\'
  545.    
  546.     ld a, (CopyDataAndRepairIncr)
  547.     inc a
  548.     ld (CopyDataAndRepairIncr), a
  549.    
  550.     jp CopyDataAndRepairLoop
  551.    
  552. CopyDataAndRepairNext:
  553.    
  554.     ld hl, transData
  555.     ld a, (CopyDataAndRepairIncr)
  556.     add a, l
  557.     ld l, a
  558.     ld (hl),\'1\'
  559.    
  560.     ld a, (CopyDataAndRepairIncr)
  561.     inc a
  562.     ld (CopyDataAndRepairIncr), a
  563.  
  564.     jp CopyDataAndRepairLoop
  565.  
  566.  
  567.     ret
  568.    
  569. CopyDEStringtoHL:
  570.     ld a, (de)
  571.     cp 0
  572.     ret z
  573.     ld (hl),a
  574.     inc hl
  575.     inc de
  576.     jp CopyDEStringtoHL
  577.    
  578.  
  579.    
  580.    
  581.     ret
  582.  
  583. pause:
  584.     ld hl, 6500
  585.     ld bc, 100
  586. pauseloop:
  587.     dec hl
  588.     ld a, l
  589.     cp 0
  590.     jp nz, pauseloopcontinue
  591.     ld a, h
  592.     cp 0
  593.     jp nz,pauseloopcontinue
  594.    
  595.     ret
  596.    
  597. pauseloopcontinue:
  598.     jp pauseloop
  599.  
  600.     ret
  601.     ;01010100
  602.     ;
  603.    
  604.    
  605. transSendPrev:
  606.     .db 0
  607.  
  608. transInc:
  609.     .db 0
  610. transData:
  611.     .ds 100
  612. transDataCompressed:
  613.     .ds 12
  614.  
  615. prev:
  616.     .db 0
  617.  
  618. tmpa:
  619.     .db 0
  620. tmpb:
  621.     .db 0
  622.    
  623. receiveDataSafeData1:
  624.     .ds 100
  625.    
  626. receiveDataSafeData2:
  627.     .ds 100
  628.    
  629. receiveDataSafeData3:
  630.     .ds 100
  631.    
  632.    
  633. CopyDataAndRepairIncr:
  634.     .db 0
  635. fin:
  636.     .db 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement