References for writing spice model code?
This is a tough field to get into.  The learning curve is extremely steep, so be warned.  Another thing I should mention upfront is that you can't build very good intrinsic device models (diode, BJT, FET, etc.) from the datasheet alone.  Very few parameters will line up one-to-one, and the charts they usually publish aren't under the conditions you need to extract the underlying model parameters.  You'll likely need to get measurement equipment and several part samples to acquire good data.
I started with the book recommended by the author of LTspice, which is Semiconductor Device Modeling with SPICE (2nd ed.) by Massobrio &amp; Antognetti.  At first, I felt completely overwhelmed by this book, but if you can just focus a bunch of time on Chapter 1 (diodes), then you'll find the later chapters are just building off that initial chapter.  Maybe reread that chapter a couple times.  The later chapters will cover actual parameter extraction techniques, but it's important to know what the parameters are doing first.
Several years later, I stumbled upon this next one which I think was very useful in its own right.  It is called SPICE: Practical Device Modeling (1st ed.) by Kielkowski.  However, it is a little outdated since it is focuses on SPICE2 while the most popular SPICE packages nowadays (except PSpice) are built off of SPICE3.  You actually need to be aware of these little quirks if your intent is to make good models with wide compatibility.  Think of PSPICE and SPICE3 to be two separate main branches off of SPICE2, while packages like LTspice and ngspice are little twigs hanging off of SPICE3.
Other than those...the built-in LTspice help is great, and the ngspice user manual is useful too.  I've had to look at the PSpice Reference Guide a couple times as well, but make sure you save that PDF because I don't know how long that link will last.  There aren't really any online resources I found particularly useful until you get to opamp macromodels, although for many opamps I prefer to use LTspice's built-in UniversalOpamp2 and adjust its parameters to fit the opamp I would like to model.  For info on that, see the example schematic found at Documents\LTspiceXVII\examples\Educational\UniversalOpamp2.asc (assuming a Windows PC installation).  Anyway, here are some honorable mentions for other macromodels:
http://www.analog.com/media/en/technical-documentation/application-notes/AN-138.pdf

http://www.ecircuitcenter.com/OpModels/OpampModels.htm

http://masteringelectronicsdesign.com/buildi-an-op-amp-spice-model-from-its-datasheet/