Skip to content

Commit

Permalink
feat(README): adds code quality badges to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepeterson committed May 27, 2024
1 parent be24eba commit b13b42d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ What good is [an 8080 CPU emulator](https://github.com/lukepeterson/go8080cpu) w
This project takes an input string, tokenises the string into a slice of tokens, then parses those tokens, converting each to a valid 8080 opcode.

[![Tests](https://github.com/lukepeterson/go8080assembler/actions/workflows/go.yml/badge.svg)](https://github.com/lukepeterson/go8080assembler/actions/workflows/go.yml)
![Go Report Card](https://goreportcard.com/badge/github.com/lukepeterson/go8080assembler)

# Features

Expand Down
10 changes: 4 additions & 6 deletions assembler/assembler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package assembler provides functions for assembling Intel 8080 CPU assembler commands.
package assembler

import (
Expand All @@ -6,6 +7,7 @@ import (
"strings"
)

// Assembler stores our assembled bytecode
type Assembler struct {
ByteCode []byte
}
Expand Down Expand Up @@ -77,10 +79,7 @@ func (a *Assembler) parseLine(line string) error {
return nil
}

// parseHex takes a string with an H suffix or 0x prefix and parses it into a 16 bit integer,
// returning the result as two int8s. This has a nice side effect of being able to take
// a one byte string and also returning the result as two 8bit integers, which is
// required for our 2 byte instructions. For example: "4AH" -> 0x00, 0x4A
// parseHex takes a string with an H suffix or 0x prefix and parses it into a 16 bit integer, returning the result as two int8s. This has a nice side effect of being able to take a one byte string and also returning the result as two 8bit integers, which is required for our 2 byte instructions. For example: "4AH" -> 0x00, 0x4A
func parseHex(token string) (uint8, uint8, error) {
token = strings.TrimSuffix(token, "H")
token = strings.TrimPrefix(token, "0x")
Expand All @@ -95,8 +94,7 @@ func parseHex(token string) (uint8, uint8, error) {
return highByte, lowByte, nil
}

// Assemble takes a newline separated string of code, parses the input into tokens,
// and then converts each instruction to a valid opcode from the instructionSet.
// Assemble takes a newline separated string of code, parses the input into tokens, and then converts each instruction to a valid opcode from the instructionSet.
func (a *Assembler) Assemble(code string) error {
lines := strings.Split(code, "\n")
for _, line := range lines {
Expand Down
7 changes: 3 additions & 4 deletions assembler/instructions.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package assembler

// Instruction stores a single instruction, made up of a one byte Opcode and an instruction length.
type Instruction struct {
Opcode byte
Length int
}

// Grouped by instruction set group as per "Table 2. Instruction Set Summary",
// in the Intel 8080A 8-BIT N-CHANNEL MICROPROCESSOR datasheet.
// Grouped by instruction set group as per "Table 2. Instruction Set Summary", in the Intel 8080A 8-BIT N-CHANNEL MICROPROCESSOR datasheet.
// Opcode = the 1 byte hexadecimal representation of the instruction for the 8080
// Length = how many bytes in total the instruction has (more succintly, how many n-1 bytes _after_
// the opcode to interpret as the operand)
// Length = how many bytes in total the instruction has (more succintly, how many n-1 bytes _after_ the opcode to interpret as the operand)
var instructionSet = map[string]Instruction{
// MOVE, LOAD AND STORE
"MOV A,B": {Opcode: 0x78, Length: 1},
Expand Down
9 changes: 2 additions & 7 deletions assembler/tokeniser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import (
"strings"
)

// tokenise takes a line of code as an input, removes extra spacing/commas,
// and then returns the opcode and operands as separate strings. The most
// important feature of this function is the ability to distinguish between
// single and multi-byte instructions and treat them as one. For example:
// "MOV B, B" (one byte) and "MVI B" (two bytes).
// tokenise takes a line of code as an input, removes extra spacing/commas, and then returns the opcode and operands as separate strings. This function can also distinguish between single and multi-byte instructions and treat them as one. For example: "MOV B, B" (one byte) and "MVI B" (two bytes).
func tokenise(line string) ([]string, error) {
line = normalise(line)

Expand All @@ -28,8 +24,7 @@ func tokenise(line string) ([]string, error) {
return tokens, nil
}

// normalise takes an input string, converts it to upper case, strips out extra spaces,
// and makes all comma formatting consistent, helping us match against out opcode map.
// normalise takes an input string, converts it to upper case, strips out extra spaces, and makes all comma formatting consistent, helping us match against out opcode map.
func normalise(line string) string {
line = strings.ToUpper(line)

Expand Down

0 comments on commit b13b42d

Please sign in to comment.