<commit_before>import Common

class MifLine(object):

	Comment = {
				"altera" : "--",
				"cadence" : "//"
	}

	def __init__(self, data=None, address = None, comment = "", instruction = None):
		self.Address = address
		self.Data = data
		self.Comment = comment
		self.Instruction = instruction

	def Altera(self):
		string = ""
		if self.Address is not None:
			if type(self.Address) is str:
				string += self.Address.replace("0x",'').zfill(4)
			else:
				string += Common.NumToHexString(self.Address, 4)
		if self.Data is not None:
			string += " : %s;" % self.Data.zfill(8)
		if self.Comment:
			string += " %% %s %%" % self.Comment

		return string

	def Cadence(self):
		string = "@"
		if self.Address is not None:
			if type(self.Address) is str:
				string += self.Address.replace("0x",'').zfill(4)
			else:
				string += Common.NumToHexString(self.Address, 4)
		if self.Data is not None:
			string += "    {}".format(self.Data.zfill(8))
		if self.Comment:
			string += " // {}".format(self.Comment)

		return string

	def ToString(self, _format):
		if _format == "altera":
			return self.Altera()
		elif _format == "cadence":
			return self.Cadence()
		else:
			Common.Error(errorMsg="Invalid format")

	def __str__(self):
		return None

class Mif(object):

	def __init__(self, _format, output, width, address, headers = [], writeZeros=False):
		self.Format = _format
		self.OutputFile = output
		self.Width = width
		self.Address = address
		self.Depth = 2**address
		self.DataRadix = "HEX"
		self.Headers = headers
		self.Data = []
		self.WriteZeros = writeZeros

	def AddData(self, data):
		if data:
			self.Data += data
		return self

	def Write(self):
		if self.Data:
			with open(self.OutputFile, "w+") as _file:
				_file.seek(0)
				_file.truncate() # Clears out the file if it exists
				_file.write("{} Assembled for RISC_721 by <NAME>\n".format(MifLine.Comment[self.Format]))

				for line in self.Headers:
					_file.write("{} {}\n".format(MifLine.Comment[self.Format], line))

				if self.Format == "altera":
					_file.write("\nWIDTH = %s;\n" % str(self.Width))
					_file.write("DEPTH = %s;\n" % str(self.Depth)) #TODO: Check to see if number of instts is less than depth
					_file.write("ADDRESS_RADIX = HEX;\n")
					_file.write("DATA_RADIX = %s;\n" % str(self.DataRadix))
					_file.write("\nCONTENT BEGIN\n")
				
				_file.write("\n")

				addressesWritten = set()
				addressCounter = 0
				for mifLine in self.Data:
					print mifLine.ToString(self.Format)
					if mifLine.Data is None and mifLine.Address is None:
						# This is just a comment
						_file.write(mifLine.ToString(self.Format).strip()+'\n')
					elif mifLine.Address is not None:
						# If there is data and an address
						_file.write(mifLine.ToString(self.Format)+'\n')
						#addressCounter+=1
					else:
						mifLine.Address = addressCounter
						_file.write(mifLine.ToString(self.Format)+'\n')
						addressCounter+=1
				
				if self.Format == "altera":<commit_msg>Added option to add uninitialized address to zero<commit_after>import Common

class MifLine(object):

	Comment = {
				"altera" : "--",
				"cadence" : "//"
	}

	def __init__(self, data=None, address = None, comment = "", instruction = None):
		self.Address = address
		self.Data = data
		self.Comment = comment
		self.Instruction = instruction

	def GetAddressAsInt(self):
		try:
			if type(self.Address) is str:
				return int(self.Address, 16)
			else:
				return self.Address
		except Exception as e:
			print e
			raise e

	def Altera(self):
		string = ""
		if self.Address is not None:
			if type(self.Address) is str:
				string += self.Address.replace("0x",'').zfill(4)
			else:
				string += Common.NumToHexString(self.Address, 4)
		if self.Data is not None:
			string += " : %s;" % self.Data.zfill(8)
		if self.Comment:
			string += " %% %s %%" % self.Comment

		return string

	def Cadence(self):
		string = "@"
		if self.Address is not None:
			if type(self.Address) is str:
				string += self.Address.replace("0x",'').zfill(4)
			else:
				string += Common.NumToHexString(self.Address, 4)
		if self.Data is not None:
			string += "    {}".format(self.Data.zfill(8))
		if self.Comment:
			string += " // {}".format(self.Comment)

		return string

	def ToString(self, _format):
		if _format == "altera":
			return self.Altera()
		elif _format == "cadence":
			return self.Cadence()
		else:
			Common.Error(errorMsg="Invalid format")

	def __str__(self):
		return None

class Mif(object):

	def __init__(self, _format, output, width, address, headers = [], writeZeros=False):
		self.Format = _format
		self.OutputFile = output
		self.Width = width
		self.Address = address
		self.Depth = 2**address
		self.DataRadix = "HEX"
		self.Headers = headers
		self.Data = []
		self.WriteZeros = writeZeros
		self.AddressesWritten = set()

	def AddData(self, data):
		if data:
			self.Data += data
		return self

	def AddAddress(self, address):
		if address in self.AddressesWritten:
			Common.Error(errorMsg="Address already written to: {}".format(address))
		else:
			self.AddressesWritten.add(address)

	def Write(self):
		if self.Data:
			with open(self.OutputFile, "w+") as _file:
				_file.seek(0)
				_file.truncate() # Clears out the file if it exists
				_file.write("{} Assembled for RISC_721 by <NAME>\n".format(MifLine.Comment[self.Format]))

				for line in self.Headers:
					_file.write("{} {}\n".format(MifLine.Comment[self.Format], line))

				if self.Format == "altera":
					_file.write("\nWIDTH = %s;\n" % str(self.Width))
					_file.write("DEPTH = %s;\n" % str(self.Depth)) #TODO: Check to see if number of instts is less than depth
					_file.write("ADDRESS_RADIX = HEX;\n")
					_file.write("DATA_RADIX = %s;\n" % str(self.DataRadix))
					_file.write("\nCONTENT BEGIN\n")
				
				_file.write("\n")

				addressCounter = 0
				for mifLine in self.Data:
					if mifLine.Data is None and mifLine.Address is None:
						# This is just a comment
						_file.write(mifLine.ToString(self.Format).strip()+'\n')
					elif mifLine.Address is not None:
						# If there is data and an address
						_file.write(mifLine.ToString(self.Format)+'\n')
						self.AddAddress(mifLine.GetAddressAsInt())
						#addressCounter+=1
					else:
						mifLine.Address = addressCounter
						_file.write(mifLine.ToString(self.Format)+'\n')
						self.AddAddress(mifLine.GetAddressAsInt())
						addressCounter+=1
				
				if self.WriteZeros:
					for a in range(0, self.Depth):
						if a not in self.AddressesWritten:
							_file.write(MifLine(data="0", address=a).ToString(self.Format)+'\n')

				if self.Format == "altera":
{'fraction_non_alphanumeric': 0.1447826745418749, 'fraction_numerical': 0.005452067242162653, 'mean_word_length': 3.1876981610653137, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3757811', 'n_tokens_mistral': 2755, 'n_tokens_neox': 2263, 'n_words': 553}