Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 NameError: NameError: global name 'BPLInstruction' is not defined
#1
Hello I am receiving these messages when running my code:

Error:
File "/home/leopoldo/cpu.py", line 28, in process_instructions instruction = self.identify_instruction (byte) File "/home/leopoldo/cpu.py", line 18, in identify_instruction instruction = BPLInstruction (byte) NameError: global name 'BPLInstruction' is not defined
The code:

def process_instructions (self):
		counter=0
		for byte in self.rom.data_bytes:
			byte = self.rom.get_byte (self.pc)
			instruction = self.identify_instruction (byte)
			if instruction is None:
				raise Exception ("Instruction not found")
			instruction.process ()
			print ("self.pc, antes: ", self.pc)
			self.pc+= instruction.instruction_length
			print ("counter: ", counter)
			print ("self.pc, depois: ", self.pc)
			print ("")
			counter = counter + 1
def identify_instruction (self, byte):
		if byte==0x78:	
			instruction = LDAInstruction (byte)
		elif byte==0xD8:
			instruction = SEIInstruction (byte)
		elif byte==0xA9:
			instruction = CLDInstruction (byte)
		elif byte==0x10:
			instruction = BPLInstruction (byte)
		else:
			instruction = None
		return instruction
class LDAInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: LDAInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 2

class SEIInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: SEIInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class CLDInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: CLDInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class BPLinstruction (Instruction):
	def process (self):
		print ("Identifier Byte: BPLinstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1
I can't identify the source of trouble because I see no difference between the code related to BPL, whether is its class definition or its elif clause on "identify_instruction" to the others instruction's code.

So I don't see what is not defined there, especially when python does not complain about any other instruction. Thanks for the help.
Quote
#2
in class definition, name is BPLinstruction (small i)
when instantiated you use capital I
colt likes this post
Quote
#3
(Oct-21-2019, 12:51 AM)Larz60+ Wrote: in class definition, name is BPLinstruction (small i)
when instantiated you use capital I

Thanks. I did changed it to
class BPLInstruction (Instruction):
instead of
class BPLinstruction (Instruction):
Nonetheless, the error message continued, exactly as before. What else can be?
Quote
#4
it's line 18!
print ("Identifier Byte: BPLinstruction:",hex (self.identity_byte), self.identity_byte)
Quote
#5
(Oct-23-2019, 11:29 PM)Larz60+ Wrote: it's line 18!
print ("Identifier Byte: BPLinstruction:",hex (self.identity_byte), self.identity_byte)

Between the quotes? Well, I changed it but the error remains.
Quote
#6
show your full code
Quote
#7
I somewhat was able to fix it through the correction of a logical bug.

The code that fix it is:

        if byte==0x78:	
			instruction = SEIInstruction (byte)
		elif byte==0xD8:
			instruction = CLDInstruction (byte)
		elif byte==0xA9:
			instruction = LDAInstruction (byte)
and not

def identify_instruction (self, byte):
		if byte==0x78:	
			instruction = LDAInstruction (byte)
		elif byte==0xD8:
			instruction = SEIInstruction (byte)
		elif byte==0xA9:
			instruction = CLDInstruction (byte)
However I added code for a new instruction with

class STAabsInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: STAabsInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 3
and code to deal with it
elif byte==0x8D:
			instruction = STAabsInstruction (byte)
and now I am receiving
Error:
File "/home/leopoldo/cpu.py", line 32, in process_instructions instruction = self.identify_instruction (byte) File "/home/leopoldo/cpu.py", line 20, in identify_instruction instruction = STAabsInstruction (byte) NameError: global name 'STAabsInstruction' is not defined
Full code for the two classes that use Instructions:

from rom import ROM
from instruction import LDAInstruction, SEIInstruction, CLDInstruction

class CPU (object):
	def __init__(self, rom_bytes):
		self.registers = []
		self.rom = ROM (rom_bytes)
		self.pc = 0		

	def identify_instruction (self, byte):
		if byte==0x78:	
			instruction = SEIInstruction (byte)
		elif byte==0xD8:
			instruction = CLDInstruction (byte)
		elif byte==0xA9:
			instruction = LDAInstruction (byte)
		elif byte==0x10:
			instruction = BPLInstruction (byte)
		elif byte==0x8D:
			instruction = STAabsInstruction (byte)
		else:
			instruction = None
		return instruction
		

	def process_instructions (self):
		counter=0
		for byte in self.rom.data_bytes:
			byte = self.rom.get_byte (self.pc)
			print ("self.pc, antes: ", self.pc)
			print ("byte: ", hex (byte), byte)
			instruction = self.identify_instruction (byte)
			if instruction is None:
				raise Exception ("Instruction not found")
			instruction.process ()
			self.pc+= instruction.instruction_length
			print ("counter: ", counter)
			print ("self.pc, depois: ", self.pc)
			print ("")
			if self.pc>=5:
				break
			
from abc import ABCMeta, abstractmethod, abstractproperty

class Instruction (object):
	__metaclass_ = ABCMeta
		
	def __init__ (self,identification_byte):
		self.identity_byte = identification_byte

	@abstractproperty
	def instruction_length (self):
		return 1

	@abstractmethod
	def process (self):
		pass
		
class SEIInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: SEIInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class CLDInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: CLDInstruction:",hex (self.identity_byte), self.identity_byte)
	instruction_length = 1

class LDAInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: LDAInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 2

class STAabsInstruction (Instruction):
	def process (self):
		print ("Identifier Byte: STAabsInstruction:",hex (self.identity_byte), self.identity_byte )
	instruction_length = 3

Quote
#8
I assume these classes are in separate files, and each named the same as the class that they hold? (you don't show the module names)
on line 2 of cpu.py, you don't import STAabsInstruction, thus the error on line 20.
colt likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  NameError 'odometer' not defined rfernandez15 1 105 Nov-25-2019, 09:57 PM
Last Post: ichabod801
  Function Annotation got NameError: name 'xxx' is not defined Lance 6 342 Oct-23-2019, 03:13 AM
Last Post: Lance
  NameError: name 'processing' is not defined soli004 2 464 Sep-07-2019, 11:57 AM
Last Post: soli004
  NameError: name 'display' is not defined when running code on power bi beginner1 2 1,811 Jul-24-2019, 11:03 AM
Last Post: beginner1
  tkinter - "NameError: name 'frame' is not defined" Mocap 3 1,184 Jul-21-2019, 09:03 PM
Last Post: Yoriz
  Global variable does not seem to be global. Columbo 6 408 Jul-15-2019, 11:00 PM
Last Post: Columbo
  [split] NameError: name 'lo' is not defined when using input ? Cedmo 2 447 Jul-05-2019, 09:39 PM
Last Post: Cedmo
  NameError: name 'lst' is not defined Jmekubo 2 851 Apr-27-2019, 12:34 PM
Last Post: Jmekubo
  *** NameError: name '' is not defined adamG 1 904 Apr-08-2019, 01:21 PM
Last Post: ichabod801
  help (variables) - NameError: name 'Payment' is not defined? Vqlk 5 591 Feb-27-2019, 04:57 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)