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
  Going thru tutorials..."NameError: name 'raw_input' is not defined" hmonnier 4 153 Jul-14-2020, 02:19 PM
Last Post: BitPythoner
  NameError for global variable leviporton 3 80 Jul-10-2020, 06:51 PM
Last Post: bowlofred
  NameError: name 'pins' is not defined glennford49 2 466 Jun-15-2020, 12:20 PM
Last Post: glennford49
  python library not defined in user defined function johnEmScott 2 179 May-30-2020, 04:14 AM
Last Post: DT2000
  [split] NameError: name 'formats' is not defined shobhu123 1 356 May-24-2020, 06:29 PM
Last Post: Skaperen
  NameError: name 'cross_validation' is not defined tmhsa 5 955 Apr-21-2020, 08:15 PM
Last Post: DT2000
  exec() in class, NameError niski1996 6 314 Apr-20-2020, 07:14 PM
Last Post: niski1996
  NameError: name 'os' is not defined lrpirlet 6 1,211 Apr-01-2020, 08:43 PM
Last Post: buran
  NameError: name ? JohnnyCoffee 4 318 Feb-24-2020, 12:32 PM
Last Post: JohnnyCoffee
  NameError: name 'edit' is not defined CabbageMan 5 651 Dec-17-2019, 04:48 PM
Last Post: CabbageMan

Forum Jump:


Users browsing this thread: 1 Guest(s)