M/C Part 13 THE BASICS ---------- * Notation * #xxxx - hexadecimal number. lsb - least significant (low) byte. msb - most significant (high) byte. D0 - data bit 0. A13 - address bit 13. n - 8-bit number. nn - 16-bit number. r - register. { A B C D E H L (HL) (IX+offset) (IY+offset) } NB. Can sometimes include lsb and msb of IX and IY. Consult your assembler's manual. rr - register pair { BC DE HL IX IY } NB. Can sometimes include SP. All instructions involving HL also exist with HL replaced by IX or IY, although only one of these three is used per instruction. Add - address. * Numbers * Processors use base 2 for their number system - binary - with only two digits, 0 and 1. The Z80 deals with either 8-bit or 16-bit numbers which give ranges of 0-255 and 0-65535 respectively. The binary digits are called bits and I'll label them from D0 (the units) to D7 or D15. When we are dealing with addresses, A0 to A15 are sometimes used. Whereas in decimal the places for each digit are worth 1, 10, 100, 1000 etc. (ie. powers of 10) the binary places are worth 1, 2, 4, 8, 16, 32 and so on. For bit Dx, the place value is 2^x. Negative numbers are represented using "2's complement,". To form the 2's complement of a number, invert all the digits and add 1. The advantage with using this system is that calculations like add and subtract still produce the right results. Fractions and large numbers are held using "Floating point representation", stored in two parts: the mantissa - the first 16 binary digits of the number, and the exponent - the power of 2 to multiply the mantissa by. The mantissa is effectively 0.xxxxxxxxxxxxxxxx where all the x's are the binary digits. Since decimal and binary are not easily converted, and binary is not very nice to look at, a third system called hexadecimal is used. This is base 16, with the digits 0-9 and A-F. A repesents ten, B eleven and so on. Of course, the place values in hex are powers of 16 - 1, 16, 256, 4096 and so on. The advantage with this is that every nibble (a nibble is a group of four bits) in binary is one hex digit. Decimal can be used to a limited degree, with a system called Binary Coded Decimal. Each decimal digit 0-9 is represented by a nibble in binary. Eg. The number 255 is represented by three nibbles - 0010 0101 0101 compared to 11111111 in binary. The Z80 will cope with additions and subtractions using this, but the command DAA must be used after every one to adjust the answer back to BCD. * Logic * There are three essential logic operations - AND, OR & XOR. These work on comparing the respective bits of two numbers, and for each bit a new digit (depending on the operation) is created. These 8 new digits then form a new number. The possible combinations for the two bits can be summed up in truth tables: Operation First bit Second Bit Result OR 0 0 0 One bit OR the 0 1 1 other. 1 0 1 1 1 1 AND 0 0 0 One bit AND the 0 1 0 other. 1 0 0 1 1 1 XOR 0 0 0 One bit or the 0 1 1 other, but not 1 0 1 both. 1 1 0 * Registers * Accumulator : A. The main register used in nearly all calculations and operations. Results tend to be stored here. General purpose: B, C, D, E, H, L. Used to supplement the accumulator. Flag register : F. Holds results of calculations:- D0 C - Carry. Set if carry results. D1 N - Negate bit. Set when subtracting. D2 P/V - Parity/Overflow. See below. D3 - D4 H - Half-carry bit used in BCD arithmetic. D5 - D6 Z - Zero. Set if zero results. D7 S - Sign. Holds msb of register. Related conditions in commands:- Z - Zero : Z = 1 NZ - Not Zero : Z = 0 C - Carry : C = 1 NC - Not Carry : C = 0 PO - Parity Odd : P/V = 1 PE - Parity Even : P/V = 0 P - Positive : S = 0 M - Negative : S = 1 Parity is useful in serial data transmissions. Even/odd refers to the number of 1's in the binary number in the accumulator. Overflow corresponds to a carry from D6 to D7. Index registers: IX, IY. 16-bit registers used mainly for holding addresses. The actual address to be used is specified by an offset which is added to the register. eg. LD (IX+3),1 Program Counter: PC. Holds the address of the next instruction. Stack Pointer : SP. Holds the top of an area of memory called the stack. It grows as data is temporarily stored (pushed) and retrieved (popped) to and from it. Naturally, the data must be retrieved in the reverse order from which it was stored. Interrupt : I. At the start of a mode 2 interrupt, this register forms the msb of the servicing routine. The port requesting the interrupt should place the lsb of this address on the data bus. Refresh : R. This is the lsb of memory currently being refreshed. Of no real use, except for generating random numbers. The 8-bit general purpose registers listed above can be paired to form 16-bit registers. ie, AF, BC, DE, HL. Here, AF isn't used in calculations, so HL takes the role of accumulator. There is also an alternate set of these eight registers, but only one bank can be in use at any one time. * Interrupts * Whenever a peripheral wants serviced, or after the TV scan (amongst others) an interrupt is generated. When this happens, the current instruction is completed, and maskable interrupts are disabled. A servicing routine is then called: Non Maskable Interrupt - cannot be switched off (masked/ disabled). Addresses #0066 Maskable Interrupts - three types, all can be disabled. Mode 0 - the address is provided on the data bus and corresponds to one of the RST addresses. Mode 1 - the address is #0038. Mode 2 - the lsb of the address is provided on the data bus, the msb is the I register. SUMMARY OF INSTRUCTIONS ----------------------- * Data Transfer * LD r,r Copy register to register. LD r,n Load number to register. LD A,r Copy register to accumulator - register can include I, and R. LD A,(addr) Load accumulator with contents of address. LD A,(rr) Load accumulator with contents of address pointed to by register pair. LD rr,nn Load number to register pair. Includes SP. LD rr,(addr) Load register pair with contents of address. Includes SP. LD (addr),r Load register to address. LD (addr),rr Load register pair to address. Stored Lo/Hi. Includes SP. LD (rr),A Load address specified by register pair with accumulator. PUSH rr Push register pair onto stack. POP rr Pop register pair off stack. LD SP,HL Load stack pointer with HL. EX HL,DE Swap round DE and HL. EX (SP),HL Swap round number at top of stack with HL. LD I,A Copy accumulator to interrupt register. LD R,A Copy accumulator to refresh register. * Change of Control Commands * JP addr Jump to address. JP x,addr Jump to address if condition x is true. All flags can be used. JP (HL) Jump to address specified by HL. CALL addr Call subroutine at address. CALL x,addr Call subroutine at address if condition x is true. All flags can be used. JR n Jump forward n locations. n is in range -128 to +127. JR x,n Jump forward n locations if condition x is true. Only zero and carry flags allowed. RET Return from subroutine. RET x Return from subroutine if condition x is true. All flags can be used. RST #0 Call routine at address given. Only addresses allowed: 0, #8, #18, #20, #28, #30, #38 DJNZ x Decrement B and jump forward x bytes (like JR) is the result is not zero. * Arithmetic Operations * INC r Increment register. INC rr Increment register pair. DEC r Decrement register. DEC rr Decrement register pair. ADD A,r Add register to accumulator. ADD A,n Add number to accumulator. ADC A,r Add register and carry to accumulator. ADC A,n Add number and carry to accumulator. ADD HL,rr Add register pair to HL. Includes SP. ADC HL,rr Add register pair and carry to HL. Includes SP. SUB A,r Subtract register from accumulator. SUB A,n Subtract number from accumulator. SBC A,r Subtract register and carry from accumulator. SBC A,n Subtract number and carry from accumulator. SBC HL,rr Subtract register pair and carry from HL. Includes SP. CP r Compare register with accumulator. CP n Compare number with accumulator. * Binary Operations * AND r AND the accumulator with register. AND n AND the accumulator with number. OR r OR the accumulator with register. OR n OR the accumulator with number. XOR r XOR the accumulator with register. XOR n XOR the accumulator with number. BIT 0,r Test bit 0 of register. Bits from 0 to 7, result in zero. (1 = NZ) SET 0,r Set bit 0 of register. Bits from 0 to 7. RES 0,r Reset bit 0 of register. Bits from 0 to 7. * Shift and Rotate Operations * RL r Rotate register to left. Carry -> D0, D7 -> Carry. RR r Rotate register to right. Carry -> D7, D0 -> Carry. RLC r Rotate register circular to left. D7 -> Carry & D0 RRC r Rotate register circular to right. D0 -> Carry & D7 SLA r Shift register to left. D7 -> Carry, 0 -> D0 SRL r Shift register to right. D0 -> Carry, 0 -> D7 SRA r Shift register to right. D0 -> Carry, D7 remains unchanged. SLL r Shift register to left. D7 -> Carry, 1 -> D0 RLA As for RL A RRA " " RR A RLCA " " RLC A RRCA " " RRC A RLD Rotate (HL) left by one nibble. MSN of A -> LSN, MSN -> MSN of A. RRD Rotate (HL) right by one nibble. LSN of A -> MSN, LSN -> LSN of A. * Input-Output Commands * IN A,(n) Read accumulator from port n. IN r,(C) Read to register from port BC. Not for (HL) or (IX+offset) etc. OUT (n),A Write to port n from accumulator. OUT (C),r Write from register to port BC. Not for (HL) or (IX+offset) etc. * Block Instructions * LDI Copy (HL) to (DE), increment HL and DE. Decrement BC. LDIR As above, but repeat until BC=0. LDD Copy (HL) to (DE), decrement HL and DE. Decrement BC. LDDR As above, but repeat until BC=0. CPI Compare (HL) with A. Zero set if same. Increment HL, decrement BC. CPIR As above, but repeat until BC=0 or zero set. CPD Compare (HL) with A. Zero set if same. Decrement HL, decrement BC. CPDR As above, but repeat until BC=0 or zero set. INI Byte read from port BC to (HL). Increment HL, decrement B. INIR As above, but repeat until B is zero. IND Byte read from port BC to (HL). Decrement HL, decrement B. INDR As above, but repeat until B is zero. OUTI Decrement B, THEN write from (HL) to port BC. Increment HL. OTIR As above, but repeat until B is zero. OUTD Decrement B, THEN write from (HL) to port BC. Decrement HL. OTDR As above, but repeat until B is zero. * Interrupt Commands * EI Enable interrupts. DI Disable interrupts. HALT Wait for interrupt. RETI Return from interrupt. RETN Return from NMI. IM0 Set interrupt mode 0. IM1 " " " mode 1. IM2 " " " mode 2. * Miscelaneous Commands * NOP No operation. Do sod all basically. CCF Complement (invert) carry flag. SCF Set carry flag. CPL Complement (invert) accumulator. NEG Negate accumulator. EXX Switch to alternate registers for BC, DE, HL. EX AF,AF' Switch to alternate registers for A and F. DAA Decimal adjust. Use after a calculation using binary-coded-decimal to get the right result.