DEV Community

Cover image for Easiest program in 6502 emulator part-2
Yukti Manoj Mulani
Yukti Manoj Mulani

Posted on

Easiest program in 6502 emulator part-2

Hello folks,
Welcome to part 2 of me trying to find what is the easiest program to write in 6502 emulator.As you read in my last blog I thought I would write a random number guessing game which was not filling the last requirement of bitmap display.

I thought about this a lot and came up to a descision. I might sound crazy and guess what I think I am going crazy. The program is very random. In simple words the program is as follows.

; input5 :: get 5 uppercase letters from the user
;
; An example of a text input routine.
;
; Chris Tyler 2023-01-12
; (C)2023 Seneca College of Applied Arts and Technology
; Licensed under the terms of the GPL v2+ - See the 
; LICENSE or COPYING file for details.
;
; Works with the 6502 emulator at http://6502.cdot.systems
;



; ROM routines
define      SCINIT      $ff81 ; initialize/clear screen
define      CHRIN       $ffcf ; input character from keyboard
define      CHROUT      $ffd2 ; output character to screen
define      SCREEN      $ffed ; get screen size
define      PLOT        $fff0 ; get/set cursor coordinates
define      ZERO_ASCII  $30

; Memory locations
define      INPUT       $2000 ; input buffer (up to 5 chars)

;
; This routine get a 5-character word from the user. Only letters
; can be accumulated. The backspace key deletes the last character
; if the character count is >0. The ENTER key accepts the input
; if the character count is 5. Lower case letters are converted to
; UPPERCASE.

    JSR SCINIT

    LDY #$00
PROMPT_CHAR:
    LDA PROMPT_TEXT,Y
    BEQ DONE_PROMPT
    JSR CHROUT
    INY
    BNE PROMPT_CHAR

DONE_PROMPT:
    JSR GET_INPUT

    LDA #$0D
    JSR CHROUT

    LDY #$00
INPUT_PRINT:
    LDA INPUT,Y
    JSR CHROUT
    INY
    CPY #$05
    BNE INPUT_PRINT
    JMP Colouring

PROMPT_TEXT:
    dcb "I","f",32,"y","o","u",32,"w","a","n","t",32,"t","o",32,"s","e","e",32
    dcb "m","a","g","i","c",32,"t","h","e","n",32,"t","y","p","e",32,"m","a","g","i","c",32,"a","n","d",32,"p","r","e","s","s",32,"e","n","t","e","r",32,"t","w","i","c","e",32,00


; ---------------------------------------------------------------

GET_INPUT:
    LDA #$A0    ; CODE FOR BLACK SPACE
    JSR CHROUT
    LDA #$83    ; CODE TO MOVE CURSOR LEFT ONE POSITION
    JSR CHROUT

    LDY #$00    ; COUNT OF CHARACTERS TYPED

GET_CHAR:
    JSR CHRIN
    CMP #$00
    BEQ GET_CHAR

    CMP #$08    ; COMPARE INPUT CHAR WITH BACKSPACE
    BNE CHECK_ENTER ; ... IF NOT BS, THEN CHECK FOR ENTER

    CPY #$00    ; COMPARE CHAR COUNT WITH 0
    BEQ GET_CHAR    ; ... IF EQUAL THEN GET ANOTHER CHAR

    DEY
    LDA #$20    ; LOAD A WITH CODE FOR WHITE SPACE
    JSR CHROUT
    LDA #$83    ; CODE TO MOVE CURSOR LEFT ONE POSITION
    JSR CHROUT
    JSR CHROUT
    LDA #$A0    ; CODE FOR BLACK SPACE
    JSR CHROUT
    LDA #$83    ; CODE TO MOVE CURSOR LEFT ONE POSITION
    JSR CHROUT

CHECK_ENTER:
    CMP #$0D    ; COMPARE INPUT CHAR WITH ENTER
    BNE CHECK_LETTER ; ... IF NOT ENTER, CHECK FOR LETTERS
    CPY #$05    ; SEE IF WE HAVE 5 CHARACTERS
    BEQ DONE_INPUT  ; IF WE HAVE 5 CHARS, AND THE USER PRESSED ENTER, DONE!

CHECK_LETTER:
    CMP #97     ; COMPARE INPUT CHAR WITH 'a'
    BCC UPPERCASE   ; ... IF LOWER, CHECK IF IT'S UPPERCASE

    CMP #123    ; COMPARE INPUT CHAR WITH 'z' + 1
    BCS GET_CHAR    ; ... IF HIGHER THAN 'z' THEN GET ANOTHER CHAR

    SEC
    SBC #32     ; SUBTRACT 32 TO CONVERT lowercase TO UPPERCASE

UPPERCASE:
    CMP #65     ; COMPARE INPUT CHAR WITH A     C=1 IF A>=65 ; C=0 IF A<65  
    BCC GET_CHAR    ; ... IF LOWER THAN 'A' THEN GET ANOTHER CHAR

    CMP #91     ; COMPARE INPUT CHAR WITH Z + 1
    BCS GET_CHAR    ; ... IF HIGHER THAN 'Z' THEN GET ANOTHER CHAR

    CPY #$05    ; CHECK THE COUNT OF CHARACTERS
    BEQ GET_CHAR    ; ... IF WE HAVE 5 WE CAN'T ACCEPT ANOTHER ONE

    STA INPUT,Y
    JSR CHROUT  ; PRINT RECEIVED LETTER ON THE SCREEN

    LDA #$A0    ; CODE FOR BLACK SPACE
    JSR CHROUT
    LDA #$83    ; CODE TO MOVE CURSOR LEFT ONE POSITION
    JSR CHROUT

    INY

    JMP GET_CHAR

DONE_INPUT:
    LDA #$20    ; LOAD A WITH CODE FOR WHITE SPACE
    JSR CHROUT

Colouring:
    lda #$07    ; colour number
        ldy #$00    ; set index to 0
loop:   sta $0200,y ; set pixel colour at page 2+y
        sta $0300,y     ; set pixel colour at page 3+y
        sta $0400,y     ; set pixel colour at page 4+y
        sta $0500,y     ; set pixel colour ar page 5+y
        iny     ; increment index
        bne loop    ; continue until done the page (256 pixels)
    jmp PROMPT_CHAR2

    LDY #$00
PROMPT_CHAR2:
    LDA Statement,Y
    BEQ Addition
    JSR CHROUT
    INY
    BNE PROMPT_CHAR2

    LDY #$00
PROMPT_CHAR3:
    LDA FinalStatement,Y
    BEQ End
    JSR CHROUT
    INY
    BNE PROMPT_CHAR3

Statement:
    dcb "1",32,"+",32,"2",32,"="
FinalStatement:
    dcb 32,"F","i","n","d",32,"o","u","t",32,"h","o","w",00

Addition:
    LDA     #01  
    CLC    
    ADC     #02 
    JSR CHROUT     
    STA     $0402
    JSR PROMPT_CHAR3

End:
    BRK

Enter fullscreen mode Exit fullscreen mode

I know it what you are thinking, it is too big!!. But guess what this is the most simple program in 6502 assembly language as far as I could find out.This program basically prompts user to enter any 5 lettered text and claims to show a magic. It is nothing but it colors the screen upon input. Then you might ask where is the arithmatic part. At the end this program tells the user that 1+2 is 4. Thats is the craziest part.

I will not tell you why it does that so easily. I want you to guess why.
Here are some hints

Comeback for the secret in the next blog!!!


The ansers are hidden in the links 🤫😉

Top comments (0)