Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6502 assembly port #2

Open
vbguyny opened this issue Jul 5, 2023 · 2 comments
Open

6502 assembly port #2

vbguyny opened this issue Jul 5, 2023 · 2 comments

Comments

@vbguyny
Copy link

vbguyny commented Jul 5, 2023

Here is a first jab at porting the code to 6502 assembly:

    ; Set initial values for r0, i0, r1, i1, r2, i2, r3, i3
    LDA #1
    STA r0
    LDA #0
    STA i0
    STA r1
    STA i1
    STA r2
    STA i2
    STA r3
    STA i3

    ; Set initial values for a0, shots
    LDA #0
    STA a0
    LDA #28
    STA shots

    ; Clear screen
    JSR $FFD2

    ; Print header
    LDX #<header
    LDY #>header
    JSR $AB1E

    ; Get gate sequence from user
    LDX #<g$
    LDY #>g$
    JSR $FFBD

    ; Print "calculating the statevector..."
    LDX #<calc_statevector
    LDY #>calc_statevector
    JSR $AB1E

    ; Loop through the gate sequence
    LDX #1
gate_loop:
    LDA g$, X
    BEQ print_results
    JSR simulate_gate
    INX
    INX
    JMP gate_loop

print_results:
    ; Print a newline character
    LDX #<newline
    LDY #>newline
    JSR $AB1E

    ; Calculate sq
    JSR calculate_sq
    JSR abs_diff
    CMP #0
    BCC exit_program

    ; Print "running <shots> iterations..."
    LDX #<running_iterations
    LDY #>running_iterations
    JSR $AB1E

    ; Reset z0, z1, z2, z3
    LDA #0
    STA z0
    STA z1
    STA z2
    STA z3

    ; Calculate p0, p1, p2, p3
    JSR calculate_p0
    JSR calculate_p1
    JSR calculate_p2
    JSR calculate_p3

    ; Loop for <shots> iterations
    LDX #0
shot_loop:
    JSR generate_random
    STA r
    JSR increment_z
    INX
    CPX shots
    BCC shot_loop

    ; Print results
    LDX #<results_00
    LDY #>results_00
    JSR $AB1E

    LDX #<z0
    LDY #>z0
    JSR $AB1E

    LDX #<print_q
    LDY #>print_q
    LDA z0
    JSR $AB1E

    ; Print more results...
    ; (Continue the pattern for other results)

exit_program:
    JMP $FFD2

simulate_gate:
    CMP #"x"
    BNE simulate_gate_x1
    JSR x0_gate
    RTS

simulate_gate_x1:
    CMP #"x"
    BNE simulate_gate_y0
    JSR x1_gate
    RTS

simulate_gate_y0:
    CMP #"y"
    BNE simulate_gate_y1
    JSR y0_gate
    RTS

simulate_gate_y1:
    CMP #"y"
    BNE simulate_gate_z0
    JSR y1_gate
    RTS

simulate_gate_z0:
    CMP #"z"
    BNE simulate_gate_z1
    JSR z0_gate
    RTS

simulate_gate_z1:
    CMP #"z"
    BNE simulate_gate_h0
    JSR z1_gate
    RTS

simulate_gate_h0:
    CMP #"h"
    BNE simulate_gate_h1
    JSR h0_gate
    RTS

simulate_gate_h1:
    CMP #"h"
    BNE simulate_gate_cx
    JSR h1_gate
    RTS

simulate_gate_cx:
    CMP #"c"
    BNE simulate_gate_sw
    JSR cx_gate
    RTS

simulate_gate_sw:
    CMP #"s"
    BNE simulate_gate_end
    JSR sw_gate
    RTS

simulate_gate_end:
    RTS

x0_gate:
    LDA r0
    STA a0
    LDA r1
    STA r0
    LDA a0
    STA r1
    LDA i0
    STA a0
    LDA i1
    STA i0
    LDA a0
    STA i1

    LDA r2
    STA a0
    LDA r3
    STA r2
    LDA a0
    STA r3
    LDA i2
    STA a0
    LDA i3
    STA i2
    LDA a0
    STA i3
    RTS

; Implement the remaining gate subroutines in a similar manner

; Subroutine to calculate the statevector normalization
calculate_sq:
    LDA r0
    STA a0
    JSR square
    LDA i0
    STA a1
    JSR square
    CLC
    LDA r1
    ADC a0
    STA a0
    LDA i1
    ADC a1
    STA a1
    JSR square
    CLC
    LDA r2
    ADC a0
    STA a0
    LDA i2
    ADC a1
    STA a1
    JSR square
    CLC
    LDA r3
    ADC a0
    STA a0
    LDA i3
    ADC a1
    STA a1
    JSR square
    RTS

abs_diff:
    LDA a0
    SEC
    SBC #1
    STA a0
    CMP #0
    BCC abs_diff_end
    CMP #0
    BCS abs_diff_end
    LDA a1
    SEC
    SBC #1
    STA a1
abs_diff_end:
    RTS

square:
    STA temp
    LDA a0
    ADC a0
    STA a0
    LDA a1
    ADC a1
    STA a1
    RTS

generate_random:
    JSR $FFC0
    STA r
    RTS

increment_z:
    LDA r0
    CMP p0
    BCC increment_z_z0
    LDA r1
    CMP p1
    BCC increment_z_z1
    LDA r2
    CMP p2
    BCC increment_z_z2
    LDA r3
    CMP p3
    BCC increment_z_z3
    RTS

increment_z_z0:
    INC z0
    RTS

; Implement the remaining increment_z subroutines in a similar manner

newline:
    .BYTE $0D, $0A, $00

header:
    .BYTE $22, "c64 quantum simulator", $22, $
    .BYTE $0D, $0A
    .BYTE $22, "created by davide gessa (dakk)", $22, $0D, $0A
    .BYTE $22, "enter gate seq (x0,x1,y0,y1,z0,z1,h0,h1,cx,sw)", $22, $0D, $0A
    .BYTE $00

running_iterations:
    .BYTE $22, "running ", $22
    .BYTE 0
    .BYTE $22, " iterations...", $22, $00

results_00:
    .BYTE $22, "results:", $22, $0D, $0A
    .BYTE "00: [", $00

print_q:
    .BYTE "Q"
    .BYTE $00

; Define variables
r0: .BYTE 0
i0: .BYTE 0
r1: .BYTE 0
i1: .BYTE 0
r2: .BYTE 0
i2: .BYTE 0
r3: .BYTE 0
i3: .BYTE 0
a0: .BYTE 0
shots: .BYTE 0
z0: .BYTE 0
z1: .BYTE 0
z2: .BYTE 0
z3: .BYTE 0
p0: .BYTE 0
p1: .BYTE 0
p2: .BYTE 0
p3: .BYTE 0
r: .BYTE 0
temp: .BYTE 0

    .END

@dakk
Copy link
Owner

dakk commented Jul 5, 2023

That's awesome @vbguyny ! You can open a pull request to add also this PoC to the repository (or if you prefer I can do that for you)

@vbguyny
Copy link
Author

vbguyny commented Jul 5, 2023

PR: #4

@vbguyny vbguyny mentioned this issue Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants