我正在尝试使用6502微控制器指令集生成两个输出:20Hz方波和30Hz方波组装。到目前为止,我可以输出20Hz波:
%uasm65,title="SQUARES"
org 0200h
lda #1d
sta 0a200h
Main:
;jump to the subroutine Delay and do it
jsr Delay
lda 0a200h
inc Count1
lda Count1
cmp #3d
beq Reset1
jmp Main
Reset1:
lda #0d
sta Count1
lda 0a200h
eor #00000001b
sta 0a200h
jmp Main
Reset2:
jmp Main
Delay:
;Save registers on the stack.
pha
txa
pha
tya
pha
;Change the number that is being loaded into the
; 'A' register in order to change the delay time.
lda #01h
OutLoop:
ldx #04h
InLoop1:
ldy #0ffh
InLoop2:
dey
bne InLoop2
dex
bne InLoop1
sec
sbc #1d
bne OutLoop
;Restore registers from the stack.
pla
tay
pla
tax
pla
rts
Count1:
dbt 0d
Count2:
dbt 0d
end
%/uasm65
根据我的理解,我能做到的是采用60Hz方波并用它来获得30Hz和20Hz。如何将20Hz方波输出到PortA的第5位,将30Hz方波输出到PortA的第6位,而不影响端口中其他位的状态?换句话说,我如何从60这里获得20和30?我是否让计数检查7并增加计数2?任何帮助将不胜感激。
答案 0 :(得分:3)
你需要2个独立的计数器,每个针脚一个
Main:
;jump to the subroutine Delay and do it
jsr Delay
lda 0a200h ; ?? what's this doing here?
inc Count1 ; count1 is for the 20 Hz bit pin
lda Count1
cmp #3d ; 60/20 = 3, so counter1 will have to reach 3
bne Skip1 ; otherwise skip toggling
toggle_pin5:
lda #0d ; reload first Counter
sta Count1
lda 0a200h
eor #00000001b
sta 0a200h
skip1:
inc Count2 ; count2 is for the 30 Hz bit pin
lda Count2
cmp #2d ; 60/30 = 2, so counter2 will have to reach 2
bne Skip2 ; you could also "bne Main" here
toggle_pin6:
lda #0d ; reload 2nd Counter
sta Count2
lda 0a200h
eor #00000010b ; you will want to change this for the correct value to "set bit6 of PortA"
sta 0a200h
skip2:
jmp Main
Reset1: ; not needed anymore
Reset2: ; not needed anymore
Delay: [ ... ]
在你的循环中,分支到Reset1(或Reset2),并且跳回Main不是一个好主意,你跳过第二个检查第二个引脚。最好只分支几条指令(就像我做的那样),或使用JSR / RET:
cmp #3d
bne SkipCall ; counter value NOT reached, so skip "Reset"
jsr Reset
SkipCall:
<...>
Reset:
lda #0d
sta Count1
<...>
ret
答案 1 :(得分:2)
根据我对the instruction set的解读,这应该比Tommylee的代码更有效。 (我用它作为起点)。
如果使用在结果达到零时设置零标志的减量,则在asm中计数朝向零是优选的。那你就不需要单独比较了。这可以将代码大小降低到0x1D
字节(对于我的第二个版本)。
我假设带有内存操作数的dec
仍然根据结果设置标志。除了维基百科,我还没有看过任何6502文档。 :P问题中的代码使用dey
/ bne
,因此我认为这是正确的并设置了标记。
假设优化较少的指令更好,您应该尝试减少延迟循环。也许只是嵌套的内存循环递减,bne
作为循环条件(所以你循环2 ^ n次)?除非使用内存需要更多功率?
Main:
ldx #3d ; 60/20 = 3: toggle every 3 iterations
; stx Count5 ; Count5 is for the 20 Hz bit wave on pin5
ldy #2d ; 60/30 = 2: toggle every 2 iteration
; sty Count6 ; Count6 is for the 30 Hz bit wave on pin6
; omit the stores: Count5 and Count6 are already initialized.
; lda 0a200h ; start with the initial state of the I/O port
lda #1d ; constant initial state
squarewave_loop:
jsr Delay
; lda 0a200h ; or do this here, so Delay doesn't have to save/restore A
dec Count1
bne skip1 ; toggle when it reaches zero
toggle_pin5:
stx Count5 ; reload first countdown counter
eor #00000001b
skip1:
dec Count2
bne skip2 ; toggle when it reaches zero
toggle_pin6:
sty Count6
eor #00000010b ; FIXME: which bit maps to bit6 of Port A?
skip2:
sta 0a200h ; always store, even if there was no state change
jmp squarewave_loop
Delay: [ ... ]
Count1:
dbt 3d
Count2:
dbt 2d
dey
/ dex
然后我们不需要任何内存来存储计数器,我假设内存操作数的指令具有更长的编码
Main:
ldx #3d ; 60/20 = 3: toggle every 3 iterations
ldy #2d ; 60/30 = 2: toggle every 2 iteration
; lda 0a200h ; start with the initial state of the I/O port
lda #1d ; constant initial state
squarewave_loop:
jsr Delay
; lda 0a200h ; or do this here, so Delay doesn't have to save/restore A
dex
bne skip1
;toggle_pin5: ; runs when 1st down-counter hits zero
ldx #3d ; reload the countdown
eor #00000001b
skip1:
dey
bne skip2
;toggle_pin6: ; runs when 2nd down-counter hits zero
ldy #2d
eor #00000010b ; FIXME: which bit maps to bit6 of Port A?
skip2:
sta 0a200h ; always store, even if there was no state change
jmp squarewave_loop
Delay: [ ... ]
如果我删除注释并从标签末尾删除:
字符,则会在http://www.masswerk.at/6502/assembler.html上进行汇编。不计算延迟循环的总大小是0x1D字节的代码。