Class Calculator

  • All Implemented Interfaces:
    Runnable, net.sourceforge.uiq3.calculator.ICalculator, net.sourceforge.uiq3.IPropertyNotifier

    public abstract class Calculator
    extends Object
    implements net.sourceforge.uiq3.calculator.ICalculator
    HP-45 Calculator Engine
    Author:
    "Martin Krischik" «krischik@users.sourceforge.net»
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface net.sourceforge.uiq3.calculator.ICalculator

        net.sourceforge.uiq3.calculator.ICalculator.AlphaRegisterType, net.sourceforge.uiq3.calculator.ICalculator.Companion, net.sourceforge.uiq3.calculator.ICalculator.DefaultImpls, net.sourceforge.uiq3.calculator.ICalculator.DisplayType
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) byte[] a
      register a, general purpose
      (package private) byte[] b
      register b, general purpose
      (package private) byte[] c
      register c, X
      private boolean Calculator_Running
      Calculator is running - needed for power saving which was not there in the original
      private boolean Calculator_Shutdown
      Calculator is not running - needed for end program.
      (package private) byte carry
      carry
      (package private) byte[] d
      register d, Y
      (package private) int del_grp  
      (package private) int del_rom  
      static String DISPLAY_TEXT_CHANGED  
      (package private) boolean displayEnable
      Display enable - used to switch off the display so that intermediate result are not shown to the user.
      private @NotNull String displayText
      currently displayed text.
      private IOperation do_op_clear_reg
      clear register operations
      private IOperation do_op_clear_s
      clear s register operation
      (package private) byte[] e
      register e, Z
      private static char[] EXPONENT_CHAR
      ⁰ SUPERSCRIPT ZERO Unicode: U+2070, UTF-8: E2 81 B0
      private static int EXPONENT_SIGN
      location of the +/- sign of the exponent inside a register or memory.
      (package private) byte[] f
      register f, T
      (package private) net.sourceforge.uiq3.ui.ICalculatorCanvas form
      Canvas class where we display our result or get our keycodes
      private int Group_Read
      Group address read from file
      (package private) static @NonNls String HP45_LST  
      (package private) int IO_Count
      Number of virtual CPU cycles until next IO run.
      private static int IO_SPEED
      Larger numbers reduce the amount of IO performed so the system has more time to run the actual calculator.
      private int Key_Flag
      Key event processing status
      private static int Key_Flag_Depressed
      Key event processing status
      private static int Key_Flag_Idle
      Key event processing status
      private static int Key_Flag_Pressed
      Key event processing status
      private static int Key_Flag_Wait
      Key event processing status
      private long Key_Time
      System time when last Key was clicked.
      private static Logger Logger
      Class logger instance.
      (package private) byte[] m
      register m, scratch pad.
      (package private) static int Max_Group
      ROM Groups
      (package private) static int Max_Ram
      RAM Size
      (package private) static int Max_Rom
      ROMs per group
      private @NotNull IOperation[] op_fcn
      array of all operator functions. not null after reset
      private int Opcode_Read
      Opcode read from file
      (package private) static int Opcode_Size
      maximum amount of opcodes
      (package private) int p  
      private int Pointer_Read
      address read from file
      (package private) byte Previous_Carry
      previous carry
      (package private) int Program_Counter  
      (package private) int Program_Group  
      (package private) int Program_Rom  
      private PropertyChangeSupport propertyChangeSupport
      Handle property changes.
      (package private) byte[][] ram
      Calculator RAM.
      (package private) int ram_addr
      ram address
      (package private) static String[] REGISTER_NAMES
      names of all registers
      (package private) int Return_Program_Counter  
      private int Rom_Read
      Rom address read from file
      (package private) static int Rom_Size
      Size of each ROM
      (package private) byte[] s
      register s
      (package private) static int S_Size
      s register size
      private static String TAG
      Class logger tag.
      (package private) short[][][] ucode
      All Rom modules
      (package private) static int Word_Size
      Size of one register.
      • Fields inherited from interface net.sourceforge.uiq3.calculator.ICalculator

        Companion
    • Constructor Summary

      Constructors 
      Constructor Description
      Calculator()  
    • Field Detail

      • EXPONENT_SIGN

        private static final int EXPONENT_SIGN
        location of the +/- sign of the exponent inside a register or memory.
        See Also:
        Constant Field Values
      • EXPONENT_CHAR

        private static final char[] EXPONENT_CHAR
        ⁰ SUPERSCRIPT ZERO Unicode: U+2070, UTF-8: E2 81 B0
      • IO_SPEED

        private static final int IO_SPEED
        Larger numbers reduce the amount of IO performed so the system has more time to run the actual calculator.
        See Also:
        Constant Field Values
      • Key_Flag_Depressed

        private static final int Key_Flag_Depressed

        Key event processing status

        Key_Flag_Idle
        Calculator is idle and ready to receive the next button from the key buffer
        Key_Flag_Pressed
        Calculator is processing the pressed event
        Key_Flag_Wait
        Waiting for the calcuator to process the event
        Key_Flag_Depressed
        Calculator is processing the de-pressed event
        See Also:
        Constant Field Values
      • Key_Flag_Idle

        private static final int Key_Flag_Idle

        Key event processing status

        Key_Flag_Idle
        Calculator is idle and ready to receive the next button from the key buffer
        Key_Flag_Pressed
        Calculator is processing the pressed event
        Key_Flag_Wait
        Waiting for the calcuator to process the event
        Key_Flag_Depressed
        Calculator is processing the de-pressed event
        See Also:
        Constant Field Values
      • Key_Flag_Pressed

        private static final int Key_Flag_Pressed

        Key event processing status

        Key_Flag_Idle
        Calculator is idle and ready to receive the next button from the key buffer
        Key_Flag_Pressed
        Calculator is processing the pressed event
        Key_Flag_Wait
        Waiting for the calcuator to process the event
        Key_Flag_Depressed
        Calculator is processing the de-pressed event
        See Also:
        Constant Field Values
      • Key_Flag_Wait

        private static final int Key_Flag_Wait

        Key event processing status

        Key_Flag_Idle
        Calculator is idle and ready to receive the next button from the key buffer
        Key_Flag_Pressed
        Calculator is processing the pressed event
        Key_Flag_Wait
        Waiting for the calcuator to process the event
        Key_Flag_Depressed
        Calculator is processing the de-pressed event
        See Also:
        Constant Field Values
      • Logger

        private static final Logger Logger

        Class logger instance.

      • TAG

        private static final String TAG

        Class logger tag.

      • REGISTER_NAMES

        static final String[] REGISTER_NAMES
        names of all registers
      • a

        final byte[] a
        register a, general purpose
      • b

        final byte[] b
        register b, general purpose
      • c

        final byte[] c
        register c, X
      • d

        final byte[] d
        register d, Y
      • e

        final byte[] e
        register e, Z
      • f

        final byte[] f
        register f, T
      • m

        final byte[] m
        register m, scratch pad.
      • ram

        final byte[][] ram
        Calculator RAM.
      • s

        final byte[] s
        register s
      • ucode

        final short[][][] ucode
        All Rom modules
      • Calculator_Running

        private boolean Calculator_Running
        Calculator is running - needed for power saving which was not there in the original
      • Calculator_Shutdown

        private volatile boolean Calculator_Shutdown
        Calculator is not running - needed for end program. If you are wondering why there is a Calculator_Running and and a Calculator_Shutdown - there is that little time between the calculator stop running and beeing full shutdown
      • displayText

        @NotNull
        private @NotNull String displayText
        currently displayed text.
      • Group_Read

        private int Group_Read
        Group address read from file
      • Key_Flag

        private int Key_Flag

        Key event processing status

        Key_Flag_Idle
        Calculator is idle and ready to receive the next button from the key buffer
        Key_Flag_Pressed
        Calculator is processing the pressed event
        Key_Flag_Wait
        Waiting for the calculator to process the event
        Key_Flag_Depressed
        Calculator is processing the de-pressed event
      • Key_Time

        private long Key_Time
        System time when last Key was clicked.
      • Opcode_Read

        private int Opcode_Read
        Opcode read from file
      • Pointer_Read

        private int Pointer_Read
        address read from file
      • Rom_Read

        private int Rom_Read
        Rom address read from file
      • do_op_clear_reg

        @Nullable
        private IOperation do_op_clear_reg
        clear register operations
      • do_op_clear_s

        @Nullable
        private IOperation do_op_clear_s
        clear s register operation
      • op_fcn

        @NotNull
        private @NotNull IOperation[] op_fcn
        array of all operator functions. not null after reset
      • displayEnable

        boolean displayEnable
        Display enable - used to switch off the display so that intermediate result are not shown to the user.
      • form

        @Nullable
        net.sourceforge.uiq3.ui.ICalculatorCanvas form
        Canvas class where we display our result or get our keycodes
      • IO_Count

        int IO_Count
        Number of virtual CPU cycles until next IO run.
      • Previous_Carry

        byte Previous_Carry
        previous carry
      • Program_Counter

        int Program_Counter
      • Program_Group

        int Program_Group
      • Program_Rom

        int Program_Rom
      • Return_Program_Counter

        int Return_Program_Counter
      • carry

        byte carry
        carry
      • del_grp

        int del_grp
      • del_rom

        int del_rom
      • p

        int p
      • ram_addr

        int ram_addr
        ram address
      • propertyChangeSupport

        private final PropertyChangeSupport propertyChangeSupport
        Handle property changes. We don't use the full feature with oldValue and new newValue as that would be overkill.
    • Constructor Detail

      • Calculator

        Calculator()
    • Method Detail

      • registerToString

        static String registerToString​(byte @NotNull [] register)
      • memoryToString

        static String memoryToString​(byte @NotNull [] register)
      • addPropertyChangeListener

        public void addPropertyChangeListener​(@NotNull
                                              PropertyChangeListener listener)
        Add a PropertyChangeListener to the listener list. The listener is registered for all properties. The same listener object may be added more than once, and will be called as many times as it is added. If listener is null, no exception is thrown and no action is taken.
        Specified by:
        addPropertyChangeListener in interface net.sourceforge.uiq3.IPropertyNotifier
        Parameters:
        listener - The PropertyChangeListener to be added
      • addPropertyChangeListener

        public void addPropertyChangeListener​(@NotNull
                                              @NotNull String propertyName,
                                              @NotNull
                                              PropertyChangeListener listener)
        Add a PropertyChangeListener for a specific property. The listener will be invoked only when a call on firePropertyChange names that specific property. The same listener object may be added more than once. For each property, the listener will be invoked the number of times it was added for that property.
        Specified by:
        addPropertyChangeListener in interface net.sourceforge.uiq3.IPropertyNotifier
        Parameters:
        propertyName - The name of the property to listen on.
        listener - The PropertyChangeListener to be added
        Since:
        1.2
      • removePropertyChangeListener

        public void removePropertyChangeListener​(@NotNull
                                                 PropertyChangeListener listener)
        Remove a PropertyChangeListener from the listener list. This removes a PropertyChangeListener that was registered for all properties. If listener was added more than once to the same event source, it will be notified one less time after being removed. If listener is null, or was never added, no exception is thrown and no action is taken.
        Specified by:
        removePropertyChangeListener in interface net.sourceforge.uiq3.IPropertyNotifier
        Parameters:
        listener - The PropertyChangeListener to be removed
      • removePropertyChangeListener

        public void removePropertyChangeListener​(@NotNull
                                                 @NotNull String propertyName,
                                                 @NotNull
                                                 PropertyChangeListener listener)
        Remove a PropertyChangeListener for a specific property. If `listener` was added more than once to the same event source for the specified property, it will be notified one less time after being removed.
        Specified by:
        removePropertyChangeListener in interface net.sourceforge.uiq3.IPropertyNotifier
        Parameters:
        propertyName - The name of the property that was listened on.
        listener - The PropertyChangeListener to be removed
        Since:
        1.2
      • getDisplayMode

        @NotNull
        public net.sourceforge.uiq3.calculator.ICalculator.DisplayType getDisplayMode()
        Current display mode
        Display_Alpha
        Displays the content of the Alpha register.
        Display_L
        Displays the content of the L0 aka X register.
        Display_Formmated
        Displays the content of the L0 aka X register inside an alpha
        Specified by:
        getDisplayMode in interface net.sourceforge.uiq3.calculator.ICalculator
        Returns:
        current display mode
      • setDisplayMode

        public void setDisplayMode​(@NotNull
                                   net.sourceforge.uiq3.calculator.ICalculator.DisplayType displayMode)
        Current display mode
        Display_Alpha
        Displays the content of the Alpha register.
        Display_L
        Displays the content of the L0 aka X register.
        Display_Formatted
        Displays the content of the L0 aka X register inside an alpha
        Specified by:
        setDisplayMode in interface net.sourceforge.uiq3.calculator.ICalculator
        Parameters:
        displayMode - new display mode
      • getDisplayText

        @NotNull
        @NotNull String getDisplayText()
        returns the currently displayed text
        Returns:
        Display_Text Test to display
      • setDisplayText

        void setDisplayText​(@NotNull
                            @NotNull String displayText)
        changes the currently displayed text
        Parameters:
        displayText - Test to display
      • setForm

        public void setForm​(@Nullable
                            net.sourceforge.uiq3.ui.ICalculatorCanvas value)
        Set Canvas to display result read keyboard
        Parameters:
        value - form in which the calculator is shown.
      • getReadKey

        int getReadKey()
        get the last key read from the keyboard buffer
        Returns:
        key code.
      • Handle_IO

        protected void Handle_IO()
        Display result and read keyboard
      • Init_Opcodes

        void Init_Opcodes()
        Initialise Opcodes
      • isRunning

        public boolean isRunning()
        Specified by:
        isRunning in interface net.sourceforge.uiq3.calculator.ICalculator
        Returns:
        true when calculator is running.
      • Is_Shutdown

        @TestOnly
        public boolean Is_Shutdown()
        Calculator is not running - needed for end program. If you are wondering why there is a Calculator_Running and and a Calculator_Shutdown - there is that little time between the calculator stop running and being fully shutdown.
        Returns:
        when calculators is shut down
      • isWaiting

        public boolean isWaiting()
        is waiting in interactive mode for user input.
        Specified by:
        isWaiting in interface net.sourceforge.uiq3.calculator.ICalculator
        Returns:
        true when in interactive mode.
      • getLastStep

        public int getLastStep()

        locate the last step to display the free steps or for debugging.

        Specified by:
        getLastStep in interface net.sourceforge.uiq3.calculator.ICalculator
        Returns:
        last step
      • Parse_Address

        private boolean Parse_Address​(@NotNull
                                      @NotNull String oct)
        Parse address field
        Parameters:
        oct - string to parse
        Returns:
        address parsed
      • Parse_Opcode

        private boolean Parse_Opcode​(@NotNull
                                     @NotNull CharSequence bin)
        Parse
        Parameters:
        bin - line to parse
        Returns:
        Opcode found
      • Read_Listing_File

        public void Read_Listing_File​(String fileName)
                               throws IOException
        This reads a zip file that is expected to contain one entry which is the HP-45 firmware listing. The instructions are parsed for later execution.
        Parameters:
        fileName - File name to read from
        Throws:
        IOException - file could not be read
      • reset

        public void reset()
        Reset the calculator
        Specified by:
        reset in interface net.sourceforge.uiq3.calculator.ICalculator
      • resetAll

        public void resetAll()

        performs a full reset by deleting ram and the cache file as well a reloading the cache file.

        Specified by:
        resetAll in interface net.sourceforge.uiq3.calculator.ICalculator
      • save

        void save​(@NotNull
                  DataOutputStream Data_Out)
           throws IOException
        Parameters:
        Data_Out - Output stream to write calculators state to.
        Throws:
        IOException - and I/O while writing to Data_Out
      • do_add

        byte do_add​(byte x,
                    byte y)
        add support operation
        Parameters:
        x - x value
        y - y value
        Returns:
        x+y
      • do_sub

        byte do_sub​(byte x,
                    byte y)
        subtract support operation
        Parameters:
        x - x value
        y - y value
        Returns:
        x-y
      • run

        public final void run()
        Start virtual CPU
        Specified by:
        run in interface net.sourceforge.uiq3.calculator.ICalculator
        Specified by:
        run in interface Runnable
        See Also:
        ICalculator.run()
      • stop

        public void stop()
        Stop virtual CPU
        Specified by:
        stop in interface net.sourceforge.uiq3.calculator.ICalculator
        See Also:
        ICalculator.stop()
      • registerToString

        @NotNull
        public @NotNull String registerToString​(int address)
      • memoryToString

        @NotNull
        public @NotNull String memoryToString​(int address)