eight_bit_computer.export module

Functionality to convert data other package friendly formats.

eight_bit_computer.export.bitstrings_to_arduino_cpp(bitstrings, rom_index, header_filename, rom_var_name)[source]

Convert rom bitstrings to arduino header cpp file.

The format of the file is:

#include "mc_rom_0.h"

extern const byte MC_ROM_0[] __attribute__ (( __section__(".fini1") )) = {
    0x00, 0x01, 0x02, 0x03,  0x04, 0x05, 0x06, 0x07,  0x08, 0x09, 0x0A, 0x0B,  0x0C, 0x0D, 0x0E, 0x0F, // 00000
    0x10, 0x11, 0x12, 0x13,  0x14, 0x15, 0x16, 0x17,  0x18, 0x19, 0x1A, 0x1B,  0x1C, 0x1D, 0x1E, 0x1F, // 00016
    ...
    ...
    0xE0, 0xE1, 0xE2, 0xE3,  0xE4, 0xE5, 0xE6, 0xE7,  0xE8, 0xE9, 0xEA, 0xEB,  0xEC, 0xED, 0xEE, 0xEF, // 32736
    0xF0, 0xF1, 0xF2, 0xF3,  0xF4, 0xF5, 0xF6, 0xF7,  0xF8, 0xF9, 0xFA, 0xFB,  0xFC, 0xFD, 0xFE        // 32752
};
extern const byte MC_ROM_0_LAST_BYTE = 0xFF;

Where the rom index +1 is used for the index of the fini part.

Parameters:
  • bitstrings (list(str)) – This of bitstrings that make up the rom.
  • rom_index (int) – Index of the rom beind written (index in the list of all the roms being written to the arduino).
  • header_filename (str) – Name of the header file, e.g. “mc_rom_0.h”.
  • rom_var_name (str) – The variable name used for the rom data in the arduino code.
Returns:

String ready to be written to cpp file

Return type:

str

eight_bit_computer.export.create_arduino_header(header_file_basename, rom_var_name)[source]

Create arduino header file

The header file looks like this:

#ifndef MC_ROM_0_H
#define MC_ROM_0_H

#include <Arduino.h>

extern const byte MC_ROM_0[];
extern const byte MC_ROM_0_LAST_BYTE;

#endif
Parameters:
  • header_file_basename (str) – The basename of the header file. E.g. if the header file is named mc_rom_0.h, the basename is mc_rom_0.
  • rom_var_name (str) – The variable name used for the rom data in the arduino code.
Returns:

String ready to be written to a file.

Return type:

str

eight_bit_computer.export.chunker(seq, chunk_size)[source]

Take a larger sequence and split it into smaller chunks.

E.g.:

chunker([0,1,2,3,4,5], 4) -> [0,1,2,3], [4,5]
Parameters:
  • seq (list) – List of things to chunk up
  • chunk_size (int) – How big each chunk should be.
Returns:

Generator that yields each chunk.

Return type:

generator

eight_bit_computer.export.write_arduino_pair(bitstrings, output_dir, file_basename, rom_var_name, rom_index)[source]

Write the header and cpp files for the arduino roms.

Parameters:
  • bitstrings (list(str)) – List of bitstrings that will make up the rom.
  • output_dir (str) – Directory (relative or absolute) to output the pair of files into.
  • file_basename (str) – Basename of the h and cpp files. The basename is the part of the file without the extention or the period.
  • rom_var_name (str) – The variable name used for the rom data in the arduino code.
  • rom_index (int) – Index of the rom to be written. This index is the index in the sequence of all roms to be written to the arduino.
eight_bit_computer.export.gen_logisim_program_file(assembly_line_infos)[source]

Generate contents for logisim files holding a program.

Parameters:assembly_line_infos (list(dict)) – List of dictionaries of information about the parsed assembly.
Returns:Content of the logisim file.
Return type:str
eight_bit_computer.export.extract_machine_code(assembly_lines)[source]

Extract machine code from assembly line dictionaries.

Parameters:assembly_lines (list(dict)) – List of assembly line info dictionaries to extract machine code from. See get_assembly_line_template() for details on what those dictionaries contain.
Returns:List of bit strings for the machine code.
Return type:list(str)
eight_bit_computer.export.extract_variables(assembly_lines)[source]

Extract variables from assembly line dictionaries.

Parameters:assembly_lines (list(dict)) – List of assembly line info dictionaries to extract variables from. See get_assembly_line_template() for details on what those dictionaries contain.
Returns:List of bit strings for the machine code. Empty list if there’s no variables
Return type:list(str)
eight_bit_computer.export.combine_mc_and_variable_bitstrings(mc_byte_bitstrings, variable_bitstrings)[source]

Combine machine code and variables into a single appropriately padded list.

Parameters:
  • mc_byte_bitstrings (list(str)) – List of bitstrings that make up the machine code.
  • variable_bitstrings (list(str)) – List of bitstrings that represent the variables.
Returns:

List of the machine code and variable bitstrings, padded to that the variables begin at byte 257.

Return type:

list(str)

eight_bit_computer.export.bitstrings_to_logisim(bitstrings)[source]

Convert bitstrigs to a logisim RAM/ROM file format.

Used to convert ROMs and machine code.

Parameters:bitstrings (list(str)) – List of bitstrings to convert to a logisim friendly format.
Returns:String ready to be written to a file.
Return type:str
eight_bit_computer.export.gen_arduino_program_h_file(h_basename)[source]

Generate header file for program for Arduino.

The header file looks like this:

#ifndef PROG_FIBONACCI_H
#define PROG_FIBONACCI_H

#include <Arduino.h>

extern const byte num_fibonacci_program_bytes;
extern const byte fibonacci_program_bytes[];

extern const byte num_fibonacci_data_bytes;
extern const byte fibonacci_data_bytes[];

extern const char fibonacci_program_name[];

#endif
Parameters:h_basename (str) – The filename (with no extension) for the file.
Returns:String ready to be written to a file.
Return type:str
eight_bit_computer.export.gen_arduino_program_cpp_file(assembly_line_infos, filename_base, h_filename)[source]

Generate cpp file for program for Arduino.

The cpp file looks like this:

#include "prog_fibonacci.h"

extern const byte num_fibonacci_program_bytes = 13;
extern const byte fibonacci_program_bytes[] PROGMEM = {
    0x39, // 000 SET A #1 (@set_initial)
    0x01, // 001 (1)
    0x3A, // 002 SET B #1
    0x01, // 003 (1)
    0x08, // 004 COPY A ACC (@fib_loop)
    0xCE, // 005 ADD B
    0x24, // 006 JUMP_IF_OVERFLOW_FLAG @set_initial
    0x00, // 007 (0)
    0x03, // 008 COPY ACC C (to display)
    0x11, // 009 COPY B A
    0x02, // 010 COPY ACC B
    0x3D, // 011 JUMP @fib_loop
    0x04  // 012 (4)
};

extern const byte num_fibonacci_data_bytes = 0;

// Needs to be at least 1 byte in this array
extern const byte fibonacci_data_bytes[] PROGMEM = {
    0x00 // Placeholder.
};

// Max of seven characters
extern const char fibonacci_program_name[] = "Fbnacci";
Parameters:
  • assembly_line_infos (list(dict)) – List of assembly line info dictionaries to extract variables from. See get_assembly_line_template() for details on what those dictionaries contain.
  • filename_base (str) – The basename (no extension) for the file. Also used as a general identifier.
  • h_filename (str) – The filename of the headerfile (including extension).
Returns:

String ready to be written to a file.

Return type:

str

eight_bit_computer.export.extract_program_file_machinecode_info(assembly_line_infos)[source]

Get necessary machine code info for arduino cpp file.

Parameters:assembly_line_infos (list(dict)) – List of assembly line info dictionaries to extract variables from. See get_assembly_line_template() for details on what those dictionaries contain.
Returns:str)): Bitstring and relevant comment for each machinecode byte.
Return type:list(dict(str
eight_bit_computer.export.extract_program_file_variable_info(assembly_line_infos)[source]

Pull out the info to write variables to program arduino cpp file.

Parameters:assembly_line_infos (list(dict)) – List of assembly line info dictionaries to extract variables from. See get_assembly_line_template() for details on what those dictionaries contain.
Returns:List of variable values and names. Spots with no variables are filled with placeholders.
Return type:list(dict)