From 6f8b7d934fb73bbf7dcea568606e6b667c8af457 Mon Sep 17 00:00:00 2001 From: VBGuyNY Date: Wed, 5 Jul 2023 05:14:37 -0400 Subject: [PATCH] Create qc.asm Port of BASIC code to 6502 assembly. --- qc.asm | 331 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 331 insertions(+) create mode 100644 qc.asm diff --git a/qc.asm b/qc.asm new file mode 100644 index 0000000..68f098a --- /dev/null +++ b/qc.asm @@ -0,0 +1,331 @@ + ; 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 + JSR $AB1E + + ; Get gate sequence from user + LDX #g$ + JSR $FFBD + + ; Print "calculating the statevector..." + LDX #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 + JSR $AB1E + + ; Calculate sq + JSR calculate_sq + JSR abs_diff + CMP #0 + BCC exit_program + + ; Print "running iterations..." + LDX #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 iterations + LDX #0 +shot_loop: + JSR generate_random + STA r + JSR increment_z + INX + CPX shots + BCC shot_loop + + ; Print results + LDX #results_00 + JSR $AB1E + + LDX #z0 + JSR $AB1E + + LDX #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