Friday, February 21, 2020

Lab4 - Data Input Form



Our group chose option 2 for Lab 4 part 1. 

1. what you need to know to solve this problem.

1) Text Area
- The emulator has a text area for data entry.
- The checkbox labeled "Text Screen" can be used to hide the character display to free up more space - for editing code.
- The text box can display an 80x25 characters at $f000-$7cff, with one byte per pixel. 
- One line has consisted from 00 to 4f. 
Printable ASCII characters will be displayed. 
- If the high-order bit is set, the character will be shown in  reverse video .

2) Routines
These routines are defined in ROM.
- SCINIT $ff81 - Initialize and clear the character display
- CHRIN $ffcf - Input one character from keyboard (returns A)
- CHROUT $ffdw - Outputs one character (A) to the screen at the current cursor position.  
                              Screen will wrap/scroll appropriately. Printable characters and cursor code
                              ($80/$81/$82/$83 for up/right/left/down) as well as RETURN ($0d) are accepted.  
                             Printable ASCII codes with the high bit set will be printed in reverse video.
- SCREEN $ffed - Returns the character screen size in the X and Y registers.
- PLOT $fff0 - gets (CARRY=1) or sets (CARRY=0) the cursor position


To use the ROM routines, these define directives may be pasted into your code:
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
You can then access the routines by name using the JSR instruction:
jsr CHROUT

Week 4 Reflection - compiler



This week I learned  compiler

Through this lecture, I understood compile option had a specific meaning.

when you compile cpp file through the matrix you use this command


    -----<         gcc++  filename -std=c++0x             >-----


let's see what it is mean.


gcc++ : When you invoke gcc, it normally does preprocess, compilation, assembly, and linking.


-std=: determine the language standard.


c++0x: the working draft of the upcoming iso c++  0x standard.


also, there are many options.



-c option: it means not to run the linker.


-o file_name: place output in file file_name


-v : print the commands executed to run the stages of compilation.


-fno-builtin: do not recognize built-in functions that do not begin with _builtin_ as prefix.


-static: on systems that support dynamic linking, this prevents linking with the shared libraries.


-g: enable debugging information


-o0: do not optimize


-f:display header information for the entire file


-s: display per-section summary information


-d:disassemble sections containing code


--source: show source code, if available, along with disassembly


The output file name is a.out which means assembly.out.


https://linux.die.net/man/1/gcc

Friday, February 14, 2020

X86_64 and AArch64


X86 64 Register and Instruction

x86-64 (also known as x64x86_64AMD64 and Intel 64[note 1]) is the 64-bit version of the x86 instruction set

Resister: a register may hold an instruction, a storage address or any kind of data. Some instructions specify registers as part of the instruction. A register must be large enough to hold an instruction.


 General-Purpose of register
It is extended of 16-bit version.
64-bit register using the ‘r’ prefix. ‘r’ stand for register,  ‘x’ stand for extended.
Ex) rax, rax, rcx, rdx, rbp(register base pointer), rsp(register stack pointer), rsi(register source index), rdi( register destination index)

       the register added for 64-bit mode are named
         r8,r9,r10,r11,r12,r13,r14,r15

         32_bit register using the ‘e’ prefix or ‘d’ suffix ex) eax, r15d
         16_bit register using no prefix or ‘w’ suffix ex) ax, r15w
          8_bit register using ‘h’ suffix or ‘l’ suffix or ‘b; suffix ex) ah, bh, al, bl, r15b



        

Usage during syscall/fuction call
         -first six arguments are in rdi, rsi,rdx,rcx,r8d,r9d; remaining arguments are on the stack
         -syscall number is in rax
         -return valure in rax
         -The called routine is expected to preserve rsp,rbp, rbx, r12, r13, r14, and r15 but
may trample any other registers.

Instruction
Register names – prefixed by %
Immediate values – prefixed by $
Indirect memory access is indicated by ( )
Hexadecimal values are indicated by a 0x prefix.
Character values are indicated by quotation mark
Data sources are given as the first argument

        

AArch64 Register and Instruction Quick Start
Aarch64 is the name for the new 64-bit ARM architecture, also known as ARMv8 or ARM64. Hardware designed for this hardware.

Register
General-Purpose registers
R0 ~ r30 : to refer generally to the register
X0 ~ x30 : for 64_bit_wide access
W0 ~ w30: for 32 _bit_wide access

31 is one or two registers depending on the instruction
         -for instructions dealing with the stack, it is the stack pointer, named rsp
         -for all other instructions, it is a zero register, which return 0 when read and discards
 data when written, named rzr

usage during syscall/function call
R0~r7 are used for arguments and return values
Syscall number is in r8
R9-r15 are for temporary values.
R16-r18 are used for intra-procedure-call ad platform values
The called routine is expected to preserve r19-r28
R29 and r30 are used as the frame register and link register

Instruction
Register named are not prefixed.
Immediate value are not prefixed with a character
Indirect memory access is indicated by [  ]
Hexadecimal values are indicated by Ox prefix..
Character values are indicated by quotation marks.
Destinations are given as the first argument.

Monday, February 10, 2020

Lab3 - Bouncing Grapic final version.



This is the final version of the Lab3.


Guide Line of Lab3
  1. Create a simple graphic in a square that is 5x5 or 7x7 pixels in size. Use the colours available in the emulator's bitmapped display. The graphic could be a ball, a happy face, a logo, an emoji, or anything else (appropriate!) that you want to use.
  2. Encode that graphic in bytes using DCB (declare constant byte) instructions.
  3. Write code to make the graphic bounce around the screen, reflecting off the edges when it hits. Note: for simplicity, it is OK if the object always bounces at 45-degree angles.
  4. Make the speed keyboard-adjustable (faster/slower) and perturb the object's path once in a while.
Challenge: randomize the starting position, and make the object bounce at angles other than 45 degrees.

This is Lab3 final code.

; define
define POINTER         $10      
define  POINTER_H  $11
define  ROW_COUNT   $13 
define  ROW            $14 
define  COLUMN    $15
define  CHECK_LINE  $16 
define  ROWFLIP_FLAG  $17
define  COLFLIP_FLAG  $18  
define  PTR_CALCULATE  $19
define  DATA_INDEX  $20
define  SCREENCOL_INDEX $21

; variables for key
define UP $80 
define DOWN $82
define RIGHT $81
define LEFT $83

; variable for last line check
define LASTLINE $23

; variables for graph
define  WIDTH   7
define  HEIGHT   7

; ======= base initialize ======
   lda    #$00 ; Set a row value
   sta   ROW
   lda   #$00  ; Set a column value
   sta   COLUMN

lda  #$00 
  sta  ROWFLIP_FLAG
  sta  COLFLIP_FLAG 

; last line check calculation
lda #$20
sec
sbc #WIDTH
sta LASTLINE

; ====== Calculation ======
; ====== Initialize for calculation ======
cal_initialize:
   lda   #$00    ; create a pointer at $10
   sta   POINTER
   lda   #$02
   sta   POINTER_H

  lda  #$00  
  sta  ROW_COUNT

  lda  #$00   
  sta  PTR_CALCULATE 

  cmp  ROW
  beq  column_count
  bne  row_count

;====== row count =======
row_count:
  lda  PTR_CALCULATE
  clc
  adc  #$20
  sta  PTR_CALCULATE

  lda  POINTER_H
  adc  #$00
  sta   POINTER_H

  inc  ROW_COUNT 

  lda  ROW
  cmp  ROW_COUNT
  bne  row_count
  beq  column_count

;===== column_count =====
column_count:
   lda  PTR_CALCULATE
   clc
   adc   COLUMN
  sta  PTR_CALCULATE

;===== store the value to pointer (calculation done) =====
   sta   POINTER

;===== draw graphic =====
; initializing for drawing graph
  lda  #$00
  sta  ROW_COUNT

  ldx  #$00   ; index for data
  ldy  #$00   ; index for screen column

; draw graph
draw:  lda  ball,x

  sta  (POINTER),y 

  inx
  iny
  cpy #WIDTH
  bne  draw

  inc  ROW_COUNT

  lda  #HEIGHT 
  cmp  ROW_COUNT
  beq  getkey_initialize
  
  lda  POINTER
  clc
  adc  #$20 
  sta  POINTER 
  lda  POINTER_H 
  adc  #$00
  sta  POINTER_H  

  ldy  #$00 
  beq  draw

; ======= get key =======
getkey_initialize:
  txa     
  sta  DATA_INDEX
  tya 
  sta  SCREENCOL_INDEX   
getkey: lda   $ff   ; get a keystroke 
  ldx  #$00  ; clear out the key buffer
  stx  $ff


   cmp   #$80  ; if not a cursor key, ignore
   bmi   getkey_done
   cmp   #$84
   bpl   getkey_done 

   cmp   #UP   ; check key == up
   bne   rightKey_check

   jsr rowPointCheck_dec   ; ... if yes, decrement ROW
   jmp   getkey_done

 rightKey_check: 
  cmp   #RIGHT   ; check key == right
   bne   downKey_check

   jsr colPointCheck_inc  ; ... if yes, increment COL
   jmp   getkey_done

 downKey_check: 
  cmp   #DOWN   ; check if key == down 
   bne   leftKey_check

   jsr rowPointCheck_inc   ; ... if yes, increment ROW
   jmp   getkey_done
  
 leftKey_check: 
  cmp   #LEFT   ; check if key == left
   bne   getkey_done

   jsr colPointCheck_dec ; ... if yes, decrement COL
   clc
   bcc   getkey_done 

getkey_done:
  ldx  DATA_INDEX
  ldy  SCREENCOL_INDEX
  jmp  check_location

; ======= Check the location ======
check_location: 
  jsr   check_top
  jsr  check_bottom
  jsr  check_right 
  jsr   check_left
  jsr  move_pointer

check_initialize: 
  lda  #$00
  sta  CHECK_LINE
  rts

check_top: 
  jsr  check_initialize 
  lda  ROW  
  cmp  #$01
  lda  CHECK_LINE
  adc  #$00
  cmp  #$00  
  beq  flip_rowFlag
  rts

check_bottom: 
  jsr  check_initialize
  lda  ROW
  cmp  LASTLINE
  lda  CHECK_LINE
  adc  #$00
  cmp  #$01
  beq  flip_rowFlag 
  rts

check_left:
  jsr  check_initialize
  lda  COLUMN
  cmp  #$01
  lda  CHECK_LINE
  adc  #$00
  cmp  #$00
  beq  flip_colFlag
  rts

check_right:
  jsr  check_initialize
  lda  COLUMN
  cmp  LASTLINE
  lda  CHECK_LINE
  adc  #$00
  cmp  #$01
  beq  flip_colFlag
  rts

; ======= Flip Row Flag ====== 
flip_rowFlag:
  lda  ROWFLIP_FLAG
  cmp  #$00 
  beq  inc_rowFlag
  bne  dec_rowFlag
  rts

inc_rowFlag:
  inc  ROWFLIP_FLAG
  rts

dec_rowFlag:
  dec  ROWFLIP_FLAG
  rts

; ======= Flip Col Flag ======
flip_colFlag:
  lda  COLFLIP_FLAG
  cmp  #$00
  beq  inc_colFlag
  bne  dec_colFlag 
  rts

inc_colFlag:
  inc  COLFLIP_FLAG
  rts 

dec_colFlag:
  dec  COLFLIP_FLAG
  rts

; ======= move the graph ======== 
move_pointer:
  jsr  row_check
  jsr  col_check
  jmp  clear

row_check:
  lda  ROWFLIP_FLAG
  cmp  #$01
  beq  rowPointCheck_dec
  bne  rowPointCheck_inc

col_check:
  lda  COLFLIP_FLAG
  cmp  #$01 
  beq  colPointCheck_dec
  bne  colPointCheck_inc

rowPointCheck_inc:
lda ROW
cmp LASTLINE
bne inc_row 
  rts

inc_row:
inc ROW
rts

rowPointCheck_dec:
lda ROW
cmp #$00
bne dec_row
  rts

dec_row:
dec ROW
rts

colPointCheck_inc:
lda COLUMN
cmp LASTLINE
bne inc_col
rts

inc_col:
  inc  COLUMN
  rts

colPointCheck_dec:
  lda  COLUMN 
cmp #$00 
bne dec_col
  rts

dec_col:
dec COLUMN
rts

; ======= clear the screen before redraw the graph =====
clear: lda  ball
  sta  POINTER 
  lda  #$02
  sta  POINTER_H
  
  ldy  #$00
  tya

clear_loop:
  sta  (POINTER),y 
  iny
  bne  clear_loop

  inc  POINTER_H
  ldx  POINTER_H 
  cpx  #$06 
  bne  clear_loop

  jsr   cal_initialize

; data constant byte
ball:
  dcb 00,03,03,03,03,03,00
  dcb 03,06,07,07,07,06,03
  dcb 03,06,07,07,07,06,03
  dcb 03,07,07,07,07,07,03
dcb 03,07,06,07,06,07,03
dcb 03,07,07,06,07,07,03
  dcb 00,03,03,03,03,03,00