Skip to content

Instantly share code, notes, and snippets.

@MrMarkB
Created February 15, 2021 01:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrMarkB/da74e3d4f475ce60ccae4cff7c3bd2e9 to your computer and use it in GitHub Desktop.
Save MrMarkB/da74e3d4f475ce60ccae4cff7c3bd2e9 to your computer and use it in GitHub Desktop.
ummodTestDriver.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 4 09:16:27 2021
@author: mark
"""
import serial
from numpy.random import randint
ser = serial.Serial("/dev/ttyUSB0", baudrate=9600)
# Test stm8 eForth via serial port
# Input
# udl - lower word of unsigned double dividend (0 - 0xFFFF)
# udh - higher word of unsigned double dividend (0 - 0xFFFF)
# un - unsigned word of divisor
# Output
# flag - boolean, True if expected result is detected
# output_str - response from stm8 of form "HEX udl udh un UM/MOD . . Quotient Remainder ok"
def checkUmMod(udl,udh,un):
input_str = "HEX {0:s} {1:s} {2:s} UM/MOD . . \n\r".format(hex(udl)[2:],hex(udh)[2:],hex(un)[2:])
ser.write(bytes(input_str, "utf-8"))
output_str = ser.readline()
try:
H, UDL, UDH, UN, OP, Dot1, Dot2, Quo, Rem, OK = output_str.split()
UD = int(0x10000 * int(UDH,16) + int(UDL,16))
if UN == b'0':
Quo_Exp = 0xFFFF
Rem_Exp = 0
else:
Quo_Exp = int(UD / int(UN,16))
if Quo_Exp >= 0x10000:
Quo_Exp = 0xFFFF
Rem_Exp = 0
else:
Rem_Exp = UD % int(UN,16)
if (Rem_Exp != int(Rem,16)) or (Quo_Exp != int(Quo,16)):
passed = False
else:
passed = True
except:
print("Can't parse: {0}".format(output_str))
passed = False
return passed, output_str, hex(Quo_Exp), hex(Rem_Exp)
if __name__ == "__main__":
for k in range(100000):
udl = randint(0x10000, size=1)[0]
udh = randint(0x10000, size=1)[0]
un = randint(0x10000, size=1)[0]
passed, output_str, Quo_Exp, Rem_Exp = checkUmMod(udl, udh, un)
if not passed:
print(k, output_str, Quo_Exp, Rem_Exp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment