Browse Source

fixes

pull/2/head
Stephen 7 months ago
parent
commit
0026b64719
15 changed files with 354 additions and 70 deletions
  1. +8
    -7
      debug/serial_test.s
  2. +17
    -19
      debug/timer_test.s
  3. +1
    -1
      os/Linker.ld
  4. +3
    -0
      os/Makefile
  5. +32
    -1
      os/drivers/serial.c
  6. +4
    -0
      os/drivers/serial.h
  7. +10
    -0
      os/lib/types.h
  8. +2
    -1
      os/main.c
  9. +37
    -0
      os/prog/#terminal.cpp#
  10. +1
    -0
      os/prog/.#terminal.cpp
  11. +36
    -0
      os/prog/terminal.cpp
  12. +17
    -0
      os/prog/terminal.h
  13. +7
    -2
      os/start.s
  14. +149
    -35
      programmer/arduino/programmer/programmer.ino
  15. +30
    -4
      programmer/host/flashRom.py

+ 8
- 7
debug/serial_test.s View File

@ -1,12 +1,13 @@
/*
Serial port is located at 15MB + 3*64KB = 0xF30000
Serial port is located at 15MB + 3*64KB + 1B = 0xF30001
Extra byte is because serial port is connected to LWR(ODD)
Note that since the port is connected to 8 bits of a 16 bit bus,
The offsets for each serial register have to be doubled.
*/
.equ SPORT_LOC, 0xF30000
.equ SPORT_LOC, 0xF30001
.global _start
.long 0x12500 /* stack ptr */
.long 0x140500 /* stack ptr */
.long _start
.org 0x400
@ -16,7 +17,7 @@ _start:
/* Setup serial */
JSR init_serial
/* Send hello world */
MOVE.L HELLO_WORLD, %a0
MOVE.L #HELLO_WORLD, %a0
JSR serial_send_string
/* Echo chars forever */
ECHO_LOOP:
@ -35,8 +36,8 @@ init_serial:
# char goes in d0
serial_send_char:
MOVE.B (SPORT_LOC + 2), %d1 /* SRA */
AND #0b00000100, %d1 /* TXRDY */
CMP #0, %d1
AND.B #0b00000100, %d1 /* TXRDY */
CMP.B #0, %d1
BEQ serial_send_char /* wait for TXRDY */
MOVE.B %d0, (SPORT_LOC + 6) /* send char */
RTS
@ -44,7 +45,7 @@ serial_send_char:
# a0 - pointer to string
serial_send_string:
MOVE.B (%a0), %d0 /* load cur char */
CMP #0, %d0 /* check if NULL */
CMP.B #0, %d0 /* check if NULL */
BEQ serial_send_string_done
JSR serial_send_char /* print char */
ADD.L #1, %a0 /* inc ptr */


+ 17
- 19
debug/timer_test.s View File

@ -19,11 +19,11 @@
(how many times the timer interrupt
has fired)
*/
.equ INT_COUNT, 0x120000
.equ INT_COUNT, 0x140000
/* Are we at 0x1000 or 0x2000? */
.equ WHEREWEAT, 0x120002
.equ WHEREWEAT, 0x140002
.long 0x12500 /* stack pointer */
.long 0x140500 /* stack pointer */
.long _start
.org 0x100
@ -32,35 +32,33 @@
.org 0x400
_start:
MOVE.W #0, INT_COUNT /* set int count to 0 */
MOVE.B #0, WHEREWEAT /* set whereweat to 0 */
MOVE.W #0, (INT_COUNT) /* set int count to 0 */
MOVE.B #0, (WHEREWEAT) /* set whereweat to 0 */
/* Enable interrupts by negating int mask */
ANDI.W #0xF8FF, %sr
JMP LOWER_LOOP
timer:
MOVE.W INT_COUNT, %d0 /* Increment INT_COUNT */
ADD.L 1, %d0
MOVE.W (INT_COUNT), %d0 /* Increment INT_COUNT */
ADD.L #1, %d0
CMP #386, %d0
BEQ timer_rollover
MOVE.W %d0, INT_COUNT
MOVE.W %d0, (INT_COUNT)
RTE /* done */
/* Timer interrupt triggered 386 times */
timer_rollover:
MOVE.B #1, %d0
MOVE.W #0, INT_COUNT /* reset counter */
MOVE.B WHEREWEAT, %d1
EOR.B %d0, %d1 /* flip WHEREWEAT */
MOVE.B %d1, WHEREWEAT
MOVE.L %sp, %a0
ADD.L #2, %a0 /* save sp+2 for later */
CMP #1, %d1
BEQ timer_upper
MOVE.W #0, (INT_COUNT) /* reset counter */
MOVE.B (WHEREWEAT), %d1
EORI.B #1, %d1 /* flip WHEREWEAT */
MOVE.B %d1, (WHEREWEAT)
CMP.B #1, %d1
BNE timer_upper /* temp */
/* switch to 0x1000 */
MOVE.L #0x1000, (%a0) /* Overwrite PC in stack */
MOVE.L #0x1000, (2, %ssp) /* Overwrite PC in stack */
RTE /* done */
timer_upper: /* switch to 0x2000 */
MOVE.L #0x2000, (%a0)
MOVE.L #0x2000, (2, %ssp)
RTE /* done */
.org 0x1000


+ 1
- 1
os/Linker.ld View File

@ -5,7 +5,7 @@ ENTRY(_start)
MEMORY
{
ROM(rx): ORIGIN = 0, LENGTH = 256k
RAM(rwx): ORIGIN = 0x120000, LENGTH = 256k
RAM(rwx): ORIGIN = 0x140000, LENGTH = 256k
IO(rw): ORIGIN = 0xF10000, LENGTH = 960K
}


+ 3
- 0
os/Makefile View File

@ -12,6 +12,9 @@ start.o: start.s
%.o: %.c %.h
m68k-elf-gcc -c $< -o $@ -ffreestanding -O2 -Wall -Wextra -msoft-float
%.o: %.cpp %.h
m68k-elf-g++ -c $< -o $@ -ffreestanding -O2 -Wall -Wextra
TESTS = tests/main.c libc/string.c
test: ${TESTS}


+ 32
- 1
os/drivers/serial.c View File

@ -1,11 +1,14 @@
#include "serial.h"
#include "../lib/types.h"
volatile char *const SERIAL_BASE = (char*)0xF30000;
volatile char *const SERIAL_BASE = (char*)0xF30001;
//The first time we access SERIAL_BASE
//it points @ MR1A
//The second time, it points @MR2A
//we don't want the compiler to optimize this
//(this might be overkill - the volatile keyword might be enough)
#pragma GCC push_options
#pragma GCC optimize("O0")
void serial_init()
@ -45,6 +48,17 @@ char serial_getchar(int port)
return *(SERIAL_BASE + 6);
}
int serial_readline(char *buffer, int port)
{
int i = 0;
char c;
while((c = serial_getchar(port)) != '\n')
{
buffer[i++] = c;
}
return i;
}
int serial_putchar(int port, char c)
{
//Only port A implemented for now;
@ -67,3 +81,20 @@ int serial_print(int port, char *s)
}
return 1;
}
void serial_writeA(char c)
{
serial_putchar(0, c);
}
char serial_readA()
{
return serial_getchar(0);
}
file_t serial_construct_file(int port)
{
// TODO take port into account
return (file_t){ .write = serial_writeA, .read = serial_readA };
}

+ 4
- 0
os/drivers/serial.h View File

@ -1,5 +1,8 @@
#ifndef SERIAL_H
#define SERIAL_H
#include "../lib/types.h"
//From MC68681 datasheet
//There are more, but I'm lazy
enum baudrate
@ -15,5 +18,6 @@ int serial_set_baud(enum baudrate b, int port);
char serial_getchar(int port);
int serial_putchar(int port, char c);
int serial_print(int port, char *s);
file_t serial_construct_file(int port);
#endif

+ 10
- 0
os/lib/types.h View File

@ -0,0 +1,10 @@
#ifndef TYPES_H
#define TYPES_H
typedef struct
{
void (*write)(char);
char (*read)();
} file_t;
#endif

+ 2
- 1
os/main.c View File

@ -12,7 +12,8 @@ void kmain()
serial_print(0, " \\ /\\ / _| |_| |\\ | |__| | | | |___| |__| | \\ /\\ / | |____| | \\ \\ | |__| |____) |\r\n");
serial_print(0, " \\/ \\/ |_____|_| \\_|_____/|_| |______\\____/ \\/ \\/ |______|_| \\_\\ \\____/|_____/\r\n");
serial_print(0, "===================================================================================================\r\n");
serial_print(0, "www.scd31.com");
serial_print(0, "www.scd31.com\r\n");
keyboard_init();
while(1) {}
}

+ 37
- 0
os/prog/#terminal.cpp# View File

@ -0,0 +1,37 @@
#include "terminal.h"
#include "../lib/types.h"
Terminal::Terminal(file_t io, bool echo)
{
this->io = io;
this->echo = echo;
char buf[512];
while(1)
{
this->print("READY.\r\n> ");
this->readline(buf);
}
}
int Terminal::readline(char *buffer)
{
int i = 0;
char c;
while((c = io.read()) != '\n')
{
buffer[i++] = c;
io.write(c);
}
io.write('\n');
return i;
}
void Terminal::print(const char *s)
{
while(*s != 0)
{
io.write(*(s++));
}
}

+ 1
- 0
os/prog/.#terminal.cpp View File

@ -0,0 +1 @@
stephen@AntergosDesktop.693474:1591413702

+ 36
- 0
os/prog/terminal.cpp View File

@ -0,0 +1,36 @@
#include "terminal.h"
#include "../lib/types.h"
Terminal::Terminal(file_t io, bool echo)
{
this->io = io;
this->echo = echo;
char buf[512];
while(1)
{
this->print("READY.\r\n> ");
this->readline(buf);
}
}
int Terminal::readline(char *buffer)
{
int i = 0;
char c;
while((c = io.read()) != '\n')
{
buffer[i++] = c;
io.write(c);
}
io.write('\n');
return i;
}
void Terminal::print(const char *s)
{
while(*s != 0)
{
io.write(*(s++));
}
}

+ 17
- 0
os/prog/terminal.h View File

@ -0,0 +1,17 @@
#ifndef TERMINAL_H
#define TERMINAL_H
#include "../lib/types.h"
class Terminal
{
file_t io;
bool echo;
int readline(char *buf);
void print(const char *s);
public:
Terminal(file_t io, bool echo);
};
#endif

+ 7
- 2
os/start.s View File

@ -36,7 +36,8 @@
MOVE.L (%sp)+, %d0
.endm
.long 0x12500 /* stack ptr */
.long 0x17FFFE /* SP at top of 256K of RAM - but it has to be an even address */
/* TODO we should be able to set it to 0x180000 */
.long _start
.org 0x100
@ -47,7 +48,10 @@ _start:
/* Enable interrupts */
ANDI.W #0xF8FF, %sr
JSR kmain
/* if we get here that's super bad */
/* this loop should never happen */
loop:
JMP loop
timer_int:
@ -65,3 +69,4 @@ timer_int:
/* return */
RTE

+ 149
- 35
programmer/arduino/programmer/programmer.ino View File

@ -1,13 +1,14 @@
#define ADDR0 22
#define DATA0 2
#define DATA8 41
#define WE 10
#define WE 10
#define OE 11
#define LWR_CE 12
#define UPR_CE 13
void setup()
{
// addr
for(int i = 0; i <= 18; i++)
{
pinMode(i + ADDR0, OUTPUT);
@ -17,48 +18,140 @@ void setup()
pinMode(OE, OUTPUT);
pinMode(LWR_CE, OUTPUT);
pinMode(UPR_CE, OUTPUT);
Serial.begin(57600);
Serial.begin(9600);
}
bool writing = false;
long numBytes = 0;
long pos = 0;
void loop()
{
if(!writing && Serial.available() >= 2)
long numBytes = 0;
long pos = 0;
while(Serial.available() < 2);
//Serial.println("awaiting magic character");
//0xBEEF means we just started
byte in1 = Serial.read();
if(in1 != 0xBE) return;
byte in2 = Serial.read();
if(in1 == 0xBE && in2 == 0xEF)
{
//0xBEEF means we just started
byte in1 = Serial.read();
byte in2 = Serial.read();
if(in1 == 0xBE && in2 == 0xEF)
{
writing = true;
//Next thing received will be # bytes
numBytes = Serial.parseInt();
Serial.read(); //next byte is garbage, used as a separator
pos = 0;
setData(OUTPUT);
digitalWrite(OE, HIGH);
digitalWrite(WE, HIGH);
digitalWrite(LWR_CE, LOW);
digitalWrite(UPR_CE, LOW);
Serial.println("OK");
}
//Serial.println("got magic character");
//Next thing received will be # bytes
numBytes = Serial.parseInt();
while(Serial.available() < 1);
Serial.read(); //next byte is garbage, used as a separator
digitalWrite(OE, HIGH);
digitalWrite(WE, HIGH);
digitalWrite(LWR_CE, LOW);
digitalWrite(UPR_CE, LOW);
setData(OUTPUT);
//Erase chip
writeByteLwr(0x5555, 0xAA);
writeByteUpr(0x5555, 0xAA);
applyByte();
writeByteLwr(0x2AAA, 0x55);
writeByteUpr(0x2AAA, 0x55);
applyByte();
writeByteLwr(0x5555, 0x80);
writeByteUpr(0x5555, 0x80);
applyByte();
writeByteLwr(0x5555, 0xAA);
writeByteUpr(0x5555, 0xAA);
applyByte();
writeByteLwr(0x2AAA, 0x55);
writeByteUpr(0x2AAA, 0x55);
applyByte();
writeByteLwr(0x5555, 0x10);
writeByteUpr(0x5555, 0x10);
applyByte();
delay(200); // Wait for chip to erase
Serial.println("OK");
}
else if(writing && Serial.available() >= 2)
int i = 0;
for(; i < numBytes - 1; i += 2)
{
writeAddr(pos++);
writeDataLwr(Serial.read());
writeDataUpr(Serial.read());
digitalWrite(WE, LOW);
delay(5);
digitalWrite(WE, HIGH);
long addr = i / 2;
while(Serial.available() < 2) {}
writeByteLwr(0x5555, 0xAA);
writeByteUpr(0x5555, 0xAA);
applyByte();
writeByteLwr(0x2AAA, 0x55);
writeByteUpr(0x2AAA, 0x55);
applyByte();
writeByteLwr(0x5555, 0xA0);
writeByteUpr(0x5555, 0xA0);
applyByte();
if(--numBytes == 0) //We're done
{
writing = false;
}
writeByteLwr(addr, Serial.read());
writeByteUpr(addr, Serial.read());
applyByte();
}
if(i < numBytes)
{
//We received an odd # of bytes
while(Serial.available() < 1);
writeByteLwr(0x5555, 0xAA);
applyByte();
writeByteLwr(0x2AAA, 0x55);
applyByte();
writeByteLwr(0x5555, 0xA0);
applyByte();
writeByteLwr(i / 2, Serial.read());
applyByte();
}
setData(INPUT);
digitalWrite(OE, LOW);
//Doesn't matter if we go a byte over
for(int i = 0; i < numBytes; i += 2) // TODO off by 1?
{
//Read the data and send it back
writeAddr(i / 2);
delayMicroseconds(1);
Serial.write(readDataLwr());
Serial.write(readDataUpr());
}
}
void writeByteLwr(long addr, byte data)
{
digitalWrite(OE, HIGH);
setData(OUTPUT);
writeAddr(addr);
writeDataLwr(data);
}
void writeByteUpr(long addr, byte data)
{
digitalWrite(OE, HIGH);
setData(OUTPUT);
writeAddr(addr);
writeDataUpr(data);
}
void applyByte()
{
digitalWrite(WE, LOW);
delayMicroseconds(20);
digitalWrite(WE, HIGH);
//delayMicroseconds(20);
}
void setData(int dir)
@ -95,4 +188,25 @@ void writeDataUpr(byte data)
digitalWrite(DATA8 + i, data & 1);
data >>= 1;
}
}
}
byte readDataLwr()
{
byte data = 0;
for(int i = 0; i < 8; i++)
{
data |= digitalRead(DATA0 + i) << i;
}
return data;
}
byte readDataUpr()
{
byte data = 0;
for(int i = 0; i < 8; i++)
{
data |= digitalRead(DATA8 + i) << i;
}
return data;
}

+ 30
- 4
programmer/host/flashRom.py View File

@ -1,16 +1,17 @@
import serial
import sys
import time
if (len(sys.argv) != 3):
print(f"Usage: {sys.argv[0]} port image")
print(f"Ex. {sys.argv[0]} /dev/ttyUSB0 firmware.bin", sys.argv[0])
print(f"Ex. {sys.argv[0]} /dev/ttyUSB0 firmware.bin")
exit(1)
ser = None
try:
ser = serial.Serial(
port=sys.argv[1],
baudrate=57600
baudrate=9600
)
except:
print("Could not open serial port")
@ -18,13 +19,38 @@ except:
ser.isOpen()
time.sleep(2)
imagefile = open(sys.argv[2], "rb")
img = imagefile.read()
imagefile.close()
print(f"Have {str(len(img))} bytes to send")
#magic hex
# magic hex
ser.write([0xBE, 0xEF])
ser.write(str(len(img)).encode())
ser.write(0x55) #garbage - is thrown away
ser.write([0x55]) # garbage - is thrown away
# throw away "OK\r\n" from arduino
ser.read()
ser.read()
ser.read()
ser.read()
ser.write(img)
print("Writing complete. Verifying result...")
i = 0
good = True
while i < len(img):
x = ser.read()
if ord(x) != img[i]:
print(img[i])
print(ord(x))
good = False
break
i += 1
if good:
print("Success! :)")
else:
print("Failure! :(")
ser.close()

Loading…
Cancel
Save