/*
sopc2dts - Devicetree generation for Altera systems

Copyright (C) 2011 Walter Goossens <waltergoossens@home.nl>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package sopc2dts.lib.components.base;

import java.util.Vector;

import sopc2dts.generators.AbstractSopcGenerator;
import sopc2dts.lib.BoardInfo;
import sopc2dts.lib.Connection;
import sopc2dts.lib.components.SopcComponentDescription;
import sopc2dts.lib.components.BasicComponent;

public class SICFlash extends BasicComponent {
	
	public SICFlash(SopcComponentDescription scd, String iName, String version) {
		super(scd, iName, version);
	}

	@Override
	public String toDtsExtrasFirst(BoardInfo bi, int indentLevel, Connection conn, Boolean endComponent)
	{
		String res = "";
		Vector<FlashPartition> vPartitions = bi.getPartitionsForChip(this.getInstanceName());
		if(vPartitions != null)
		{
			if(vPartitions.size()>0)
			{
				res += AbstractSopcGenerator.indent(indentLevel) + "#address-cells = <1>;\n" +
					AbstractSopcGenerator.indent(indentLevel) + "#size-cells = <1>;\n";
			}
		}
		return res;
	}
	
	@Override
	public String toDtsExtras(BoardInfo bi, int indentLevel, Connection conn, Boolean endComponent)
	{
		int bankw;
		try {
			bankw = Integer.decode(getParamValByName("dataWidth"))/8;
		}catch(Exception e) {
			//Default to 16bit on failure
			bankw = 2;
		}
		String res = AbstractSopcGenerator.indent(indentLevel) + "bank-width = <"+bankw+">;\n" +
					AbstractSopcGenerator.indent(indentLevel) + "device-width = <1>;\n" +
					partitionsForDts(bi, indentLevel);

		return res;	
	}
	protected String partitionsForDts(BoardInfo bi, int indentLevel)
	{
		String res = "";
		Vector<FlashPartition> vPartitions = bi.getPartitionsForChip(this.getInstanceName());
		if(vPartitions != null)
		{
			for(FlashPartition part : vPartitions)
			{
				res += AbstractSopcGenerator.indent(indentLevel++) + part.getName() + '@' + Integer.toHexString(part.getAddress()) + " {\n" +
						AbstractSopcGenerator.indent(indentLevel) + String.format("reg = < 0x%08X 0x%08X >;\n", part.getAddress(),part.getSize());
				if(part.isReadonly())
				{
					res += AbstractSopcGenerator.indent(indentLevel) + "read-only;\n";
				}
				res += AbstractSopcGenerator.indent(--indentLevel) + "};\n";
			}
		}
		return res;
	}
}

@@ -1854,16 +1854,20 @@
 nt bankw
+ = 2
 ;%0A%09%09try 
@@ -1875,31 +1875,21 @@
 %0A%09%09%09
-bankw = Integer.decode(
+String sdw = 
 getP
@@ -1918,12 +1918,71 @@
 th%22)
-)/8;
+;%0A%09%09%09if(sdw!=null)%0A%09%09%09%7B%0A%09%09%09%09bankw = Integer.decode(sdw)/8;%0A%09%09%09%7D
 %0A%09%09%7D

Check for existence of param before using it