SQ

From Esolang
Jump to navigation Jump to search

SQ is esoteric programming language created by CatLooks without conditional jumps. The file with code must be exactly 32 Kb. Because a file is binary, source code can be compiled into one (compiler indev).

Code

SQ is designed to emulate 8-bit CPU (like 6502). There are lots of features that present in both.

Syntax

Source code resembles 6502 Assembly (most opcodes from 6502 are not present but there are few new ones). For example:

 label:
   lda #A9

There is one difference in syntax, namely no need for $ sign to mark hexadecimal numbers.

Registers

There are 6 registers to work with: A (Accumulator), B (Buffer), X (IndeX), I (Instruction pointer), S (Stack pointer), P (Processor status). All of them are 8-bit except for I (16-bit) and P (3-bit).

  • Accumulator is used mainly for data transfer and math.
  • Buffer is used to record Accumulator state and restore it later.
  • Index is used to set offset for Accumulator load.
  • Instruction pointer contains a pointer to current opcode.
  • Stack pointer contains a pointer to current object on stack.
  • Processor status contains some info about last operation details.

Status Register

When pushed to the stack, status register will have this form:

 -----NZC
  • C - carry (usually math overflow bit or shifted out bit)
  • Z - zero (set when number last accessed register is zero)
  • N - negative (copy of bit 7 of last accessed register)

Parameter Modes

There are only 52 opcodes (from 256 available values) in SQ (often implied or with register). They are either followed by an argument, an argument with x or just single. Here's the list of all possible argument variations:

Name Syntax Bits Description
Immediate (#) #byte 000 Straight usage of the number.
Implied - 001 No parameter.
Indirect (ind) (word) 010 Value at address located at parameter address.
Indirect with X (ind, x) (word, x) 011 Same as Indirect except parameter address is shifted with X.
Zeropage (zp) byte 100 Value at first 256 bytes of address space (RAM).
Zeropage with X (zp, x) byte, x 101 Same as Zeropage except address is shifted with X.
Absolute (abs) word 110 Value at parameter address.
Absolute with X (abs, x) word, x 111 Same as Absolute except address is shifted with X.

Note: unlike 6502, Zeropage with X mode can cross 256-byte page.

Opcodes

Here's the list of all opcodes in SQ:

Instruction Opcode Description
brk 00 Exits program.
lda # A0 Loads A with direct value.
lda abs A6 Loads A with value at address.
lda abs, x A7 Loads A with value at address + X.
lda zp A4 Loads A with value at zeropage address.
lda zp, x A5 Loads A with value at zeropage address + X.
lda (ind) A2 Loads A with value at address at address.
lda (ind, x) A3 Loads A with value at address at (address + X).
lda (ind), x AF Loads A with value at (address at address) + X.
ldx # E0 Loads X with direct value.
ldx abs E6 Loads X with value at address.
ldx zp E4 Loads X with value at zeropage address.
ldx (ind) E2 Loads X with value at address at address.
sta abs FE Stores A at address.
sta abs, x FF Stores A at address + X.
sta zp FC Stores A at zeropage address.
sta zp, x FD Stores A at zeropage address + X.
sta (ind) F2 Stores A at address at address.
stx abs 8E Stores X at address.
stx zp 8C Stores X at zeropage address.
stx (ind) 82 Stores X at address at address.
jmp abs 06 Loads I with 16-bit value.
jmp (ind, x) 03 Loads I with 16-bit value at address + X.
jsr abs 0E Pushes I on the stack and jumps to address.
rts 09 Loads I with 16-bit value poped from the stack.
tab 41 Records value of A.
rea 51 Restores value of A.
lsr # 60 Shifts A right # times (shifted bit -> carry).
lsl # 68 Shifts A left # times (shifted bit -> carry).
lsr 61 Shifts A right 1 time (shifted bit -> carry).
lsl 69 Shifts A left 1 time (shifted bit -> carry).