File:CARDIAC emulator running power of two program.gif

From HandWiki

CARDIAC_emulator_running_power_of_two_program.gif(700 × 500 pixels, file size: 7.19 MB, MIME type: image/gif, looped, 285 frames, 57 s)

Note: Due to technical limitations, thumbnails of high resolution GIF images such as this one will not be animated.

This file is from a shared repository and may be used by other projects. The description on its file description page there is shown below.

Summary

Description
English: A CARDIAC emulator in Python (see code below; requires PyGame) running a power of two program. Memory contents are shown in green with the current program counter in yellow. Program output is in white on the right side. At the bottom, the current program counter (PC), accumulator (AC), instruction (INST), and program step (STEP) are printed. The program runs for 277 steps and outputs 1, 2, 4, 8, 16, 32, 64, 128, 256, and 512. (The subroutine at location 80 prints the current value: Memory location 86 holds the next value to be printed by the output command in location 83. Location 90 has a subroutine to double the value.) The font used is Courier Prime.
Date
Source Own work
Author Morn
GIF development
InfoField
 
This GIF graphic was created with Python.
Source code
InfoField

Python code

Source code
#!/usr/bin/python

import pygame
import sys, time
from array import array

PNG = False  # save frames as PNG?
FONT = "/usr/share/fonts/TTF/Courier Prime.ttf" # font path

# power of 2 CARDIAC program
cards = """002
800
005
009
010
100
011
880
012
604
013
105
014
700
015
321
016
605
017
104
018
890
019
880
020
812
021
900
080
686
081
199
082
685
083
586
084
186
090
696
091
199
092
695
093
196
094
296
002
810"""

cards = cards.splitlines()
M = array('i', [0] * 100)   # create memory array
M[0] = 1    # start program by reading the first card
A = 0       # accumulator
PC = 0      # program counter
RES = 700, 500  # window size

INST = "INP", "CLA", "ADD", "TAC", "SFT", "OUT", "STO", "SUB", "JMP", "HRS"

pygame.init()
screen = pygame.display.set_mode(RES)
pygame.display.set_caption('PyCardiac')
font = pygame.font.Font(FONT, 20)
font2 = pygame.font.Font(FONT, 26)
output = ["Output:"]
step = 0

def getop(x):
    "Get opcode"
    if x == 0: quit()
    op = x // 100
    arg = x % 100
    return op, arg

def c(n, x):
    "Clip value x to n digits (also works correctly for negative numbers)"
    if x < 0: s = -1
    else:     s =  1
    return s * (abs(x) % (10**n))

def quit():
    "An error occurred; exit and dump memory"
    for n in range(100):
        print("M", n, M[n])
    sys.exit()

while True:
    step += 1
    op, arg = getop(M[PC])

    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    for y in range(25):
        for x in range(4):
            i = 25 * x + y
            if i == PC: co = 255, 255, 0
            else:       co = 0, 255, 0
            t = "%02u: %+04i" % (i, M[i])
            tr = font.render(t.replace("+", " "), True, co)
            screen.blit(tr, (140*x, 16*y))

    t = "PC: %02u AC: %+05i INST: %s %02u STEP: %5u" % (PC, A, INST[op], arg, step)
    tr = font2.render(t.replace("+", " "), True, (255, 0, 0))
    screen.blit(tr, (0, 450))

    for n, o in enumerate(output):
        tr = font2.render(o.replace("+", " "), True, (255, 255, 255))
        screen.blit(tr, (550, n*40))

    if PNG:
        pygame.image.save(screen, "c%05u.png" % step)
        if op == 9: # HALT
            for i in range(step + 1, step + 9):
                pygame.image.save(screen, "c%05u.png" % i)

    pygame.display.flip()
    PC += 1

    if op == 0:
        x = int(cards.pop(0))
        M[arg] = x
    if op == 1:
        A = M[arg]
    if op == 2:
        A += M[arg]
        A = c(4, A)
    if op == 3:
        if A < 0:
            PC = arg
    if op == 4:
        if A < 0: s = -1
        else:     s =  1
        left, right = arg // 10, arg % 10
        A = abs(A)  # strip sign so it works for negative numbers too
        A *= (10**left) % 10000
        A /= (10**right)
        A = s * int(A) # restore sign
        A = c(4, A)
    if op == 5:
        print(M[arg])
        output.append("%+04i" % M[arg])
    if op == 6:
        M[arg] = c(3, A)
    if op == 7:
        A -= M[arg]
        A = c(4, A)
    if op == 8:
        M[99] = 800 + PC    # save program counter
        PC = arg
    if op == 9:
        PC = arg
        # wait for the user to close the program window when halting
        while True:
            time.sleep(.1)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
    time.sleep(.2)

# use this command-line to create the animated GIF with ImageMagick:
# convert -delay 20 c0*.png -loop 0 cardiac_pow2.gif

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Captions

Add a one-line explanation of what this file represents

27 March 2023

image/gif

bc59bdad2845714ef076691a0be871174568cb9f

7,535,541 byte

57.00000000000027 second

500 pixel

700 pixel

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current05:18, 28 March 2023Thumbnail for version as of 05:18, 28 March 2023700 × 500 (7.19 MB)imagescommonswiki>Mornlonger pause at end

The following file is a duplicate of this file (more details):

The following page uses this file: