当前位置: 首页>>代码示例>>Python>>正文


Python Memory.push方法代码示例

本文整理汇总了Python中memory.Memory.push方法的典型用法代码示例。如果您正苦于以下问题:Python Memory.push方法的具体用法?Python Memory.push怎么用?Python Memory.push使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在memory.Memory的用法示例。


在下文中一共展示了Memory.push方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from memory import Memory [as 别名]
# 或者: from memory.Memory import push [as 别名]
class Controller:
    def __init__(self):
        """Model of a standard contoller unit demonstrating the Texas 4-step (fetch,decode,execute,store) with methods for each."""
        self.R1 = 0  # General purpose register to store final result.
        self.R2 = 0  # General purpose register to store file length.
        self.R3 = ""  # Storage register to store mnemonic from memory.
        self.IR = 0  # Instruction register
        self.PC = 0  # Program counter/accumulator
        self.running = False  # Machine state
        self.clock = time.time()  # Start system clock
        self.ALU = ALU()  # Arithmetic-logic units
        self.MEM = Memory()  # System memory

    def loader(self, cfile):
        """Load file data into memory prior 4-step and store it into gp register R2."""
        io("Loading " + cfile + "...")
        self.R2 = self.MEM.loader(cfile)
        io("Processed: " + str(self.R2) + " lines.")
        io("Machine is running...")
        self.running = True

    def fetch(self):
        """Fetch next instruction stored in memory using PC address and store it into storage register R3."""
        self.R3 = self.MEM.read(self.PC)
        io("|FETCH|--> address(" + str(self.PC) + ")")
        io(">read(" + str(self.PC) + "->" + self.R3 + ")")

    def decode(self):
        """Decode data fetched. Determine if valid value or instruction, bump accumulator, and store in inst register IR."""
        self.PC += 1
        try:
            self.IR = int(self.R3)
            self.R3 = "push"
            io("\t|DECODE|--> decoding(" + str(self.IR) + ")...")
            io("\t>found operand(" + str(self.IR) + ")")
            io("\t>valid instruction")
        except ValueError:
            self.IR = self.R3
            io("\t|DECODE|--> decoding(" + self.IR + ")...")
            io("\t>found operator(" + self.IR + ")")
            io("\t>valid instruction")

    def execute(self):
        """Execute instruction fetched from memory and operate/manage stack memory."""
        op = self.R3
        if op == "push":
            io("\t\t|EXECUTE|--> " + op)
            io("\t\t>pushed(" + str(self.IR) + ")")
            self.MEM.push(self.IR)
        else:
            op1, op2 = self.MEM.pop()
            io("\t\t|EXECUTE|--> " + op + "(" + str(op1) + "," + str(op2) + ")")
            io("\t\t>pop  <-(" + str(op1) + ")")
            io("\t\t>pop  <-(" + str(op2) + ")")
            self.R1 = self.ALU.operate(op, op1, op2)
            io("\t\t>push ->(" + str(self.R1) + ")")
            self.MEM.push(self.R1)

    def store(self):
        """Store resulting data, output value that is stored in register."""
        io("\t\t\t|STORE|--> storing(" + str(self.R1) + ")")

    def run(self):
        """Start steppin. Begin the Texas 4-step, calculate/display the total processing time, and display final result."""
        while self.running and self.PC < self.R2:
            io("=" * 62)
            self.fetch()
            self.decode()
            self.execute()
            self.store()
        io(
            "=" * 62
            + "\nResult:\t"
            + str(self.R1)
            + "\nTime  :\t"
            + str(round(time.time() - self.clock, 4))
            + " s\n"
            + "=" * 62
        )
开发者ID:yebra06,项目名称:RPNv2-CPU-Simulator,代码行数:81,代码来源:controller.py


注:本文中的memory.Memory.push方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。