Help: OUT (port), A

edited January 2009 in Development
[deleted]
Post edited by Unknown User on

Comments

  • edited December 2008
    port=254                 ; port 254 controls speaker
    LD B, 255     ; 6,255    ; Prepare to loop 255 times
    LD A, 0       ; 62,0     ; Prepare to send 0 to port
    out1:
    OUT (port),A  ; 211,254  ; Send 0 to speaker port in bit 4
    DJNZ, out1    ; 16,252   ; Delay by looping 255 times
    
    LD B, 255     ; 6,255    ; Prepare to loop 255 times
    LD A, 16      ; 62,16    ; Prepare to send 16 to port
    out2:
    OUT (port),A  ; 211,254  ; Send 16 to speaker port in bit 4
    DJNZ, out2    ; 16,252   ; Delay by looping 255 times
    RET           ; 201      ; Finish
    
    I don't understand the connection between the B register and the port... examples (using IN) show ports as having 2 byte addresses.
    There is no connection in the code used. OUT (BC),reg/IN reg,(BC) uses the BC register to specify the port, whereas OUT (port),A/IN A,(port) as in the example uses the immediate byte to specify the port.

    All I/O ports are 16-bit addresses, but many devices only decode the lower 8 bits of the address, so it is irrelavent what is in the top 8 bits. For instance, OUT FFFE and OUT 00FE will both access the border/speaker/ear control. IN xxFE uses the top 8 bits to select the keyboard row to read.

    OUT (BC)/IN (BC) uses the BC register to specify the I/O port. OUT (n)/IN (n) uses the suppied n for the bottom 8 bits and the contents of the A register for the top 8 bits. So, LD A,7F:IN A,(FE) reads from 7FFE, reading the keyboard row that includes the SPACE key.





    mmmmmmmmmmmmmmmmmmmm
  • edited December 2008
    JimTheBrit wrote: »
    Thanks for the reply. So the out1 loop executes 255 times, each time sending 0 to the speaker then a further 255 times (out2), this time sending a 16 out and a 1 to the speaker with each iteration? Is that correct?

    Yes, that's right
  • edited January 2009
    JimTheBrit wrote: »
    Thanks for the reply. So the out1 loop executes 255 times, each time sending 0 to the speaker then a further 255 times (out2), this time sending a 1 to the speaker with each iteration? Is that correct?

    It doesn't really have to repeatedly OUT to the speaker port, as it's the transition from 1 to 0 or 0 to 1 that produces the click. The following would work just as well:
    LD A,0
    OUT (254),A
    LD B,255
    .lp1
    DJNZ lp1
    LD A,16
    OUT (254),A
    LD B,255
    .lp2
    DJNZ lp2
    RET
    

    It's probably the distance of the LD B from the loop that makes the code confusing. In the above code I've set up B immediately before using it in the loop, rather than some time before getting there.

    In fact, if you loop 256 times instead of 255 times, then you can take advantage of B ended up as zero to not need to reload it:
    XOR  A        ; To set speaker to 0
    LD   B,A      ; B=0 to loop 256 times
    OUT  (254),A  ; Set speaker to 0
    .lp1
    DJNZ lp1      ; Wait by looping 256 times
    LD   A,16     ; To set speaker to 1
    OUT  (254),A  ; Set speaker to 1
    .lp2
    DJNZ lp2      ; Wait by looping 256 times
    RET
    
Sign In or Register to comment.